1  /* SPDX-License-Identifier: GPL-2.0-only */
2  /*
3   * AMD Secure Processor driver
4   *
5   * Copyright (C) 2017-2019 Advanced Micro Devices, Inc.
6   *
7   * Author: Tom Lendacky <thomas.lendacky@amd.com>
8   * Author: Gary R Hook <gary.hook@amd.com>
9   * Author: Brijesh Singh <brijesh.singh@amd.com>
10   */
11  
12  #ifndef __SP_DEV_H__
13  #define __SP_DEV_H__
14  
15  #include <linux/device.h>
16  #include <linux/spinlock.h>
17  #include <linux/mutex.h>
18  #include <linux/list.h>
19  #include <linux/wait.h>
20  #include <linux/dmapool.h>
21  #include <linux/hw_random.h>
22  #include <linux/bitops.h>
23  #include <linux/interrupt.h>
24  #include <linux/irqreturn.h>
25  
26  #define SP_MAX_NAME_LEN		32
27  
28  #define CACHE_NONE			0x00
29  #define CACHE_WB_NO_ALLOC		0xb7
30  
31  #define PLATFORM_FEATURE_DBC		0x1
32  #define PLATFORM_FEATURE_HSTI		0x2
33  
34  #define PSP_FEATURE(psp, feat)	(psp->vdata && psp->vdata->platform_features & PLATFORM_FEATURE_##feat)
35  
36  /* Structure to hold CCP device data */
37  struct ccp_device;
38  struct ccp_vdata {
39  	const unsigned int version;
40  	const unsigned int dma_chan_attr;
41  	void (*setup)(struct ccp_device *);
42  	const struct ccp_actions *perform;
43  	const unsigned int offset;
44  	const unsigned int rsamax;
45  };
46  
47  struct sev_vdata {
48  	const unsigned int cmdresp_reg;
49  	const unsigned int cmdbuff_addr_lo_reg;
50  	const unsigned int cmdbuff_addr_hi_reg;
51  };
52  
53  struct tee_vdata {
54  	const unsigned int cmdresp_reg;
55  	const unsigned int cmdbuff_addr_lo_reg;
56  	const unsigned int cmdbuff_addr_hi_reg;
57  	const unsigned int ring_wptr_reg;
58  	const unsigned int ring_rptr_reg;
59  	const unsigned int info_reg;
60  };
61  
62  struct platform_access_vdata {
63  	const unsigned int cmdresp_reg;
64  	const unsigned int cmdbuff_addr_lo_reg;
65  	const unsigned int cmdbuff_addr_hi_reg;
66  	const unsigned int doorbell_button_reg;
67  	const unsigned int doorbell_cmd_reg;
68  
69  };
70  
71  struct psp_vdata {
72  	const struct sev_vdata *sev;
73  	const struct tee_vdata *tee;
74  	const struct platform_access_vdata *platform_access;
75  	const unsigned int cmdresp_reg;
76  	const unsigned int cmdbuff_addr_lo_reg;
77  	const unsigned int cmdbuff_addr_hi_reg;
78  	const unsigned int feature_reg;
79  	const unsigned int inten_reg;
80  	const unsigned int intsts_reg;
81  	const unsigned int bootloader_info_reg;
82  	const unsigned int platform_features;
83  };
84  
85  /* Structure to hold SP device data */
86  struct sp_dev_vdata {
87  	const unsigned int bar;
88  
89  	const struct ccp_vdata *ccp_vdata;
90  	const struct psp_vdata *psp_vdata;
91  };
92  
93  struct sp_device {
94  	struct list_head entry;
95  
96  	struct device *dev;
97  
98  	struct sp_dev_vdata *dev_vdata;
99  	unsigned int ord;
100  	char name[SP_MAX_NAME_LEN];
101  
102  	/* Bus specific device information */
103  	void *dev_specific;
104  
105  	/* I/O area used for device communication. */
106  	void __iomem *io_map;
107  
108  	/* DMA caching attribute support */
109  	unsigned int axcache;
110  
111  	/* get and set master device */
112  	struct sp_device*(*get_psp_master_device)(void);
113  	void (*set_psp_master_device)(struct sp_device *);
114  	void (*clear_psp_master_device)(struct sp_device *);
115  
116  	bool irq_registered;
117  	bool use_tasklet;
118  
119  	unsigned int ccp_irq;
120  	irq_handler_t ccp_irq_handler;
121  	void *ccp_irq_data;
122  
123  	unsigned int psp_irq;
124  	irq_handler_t psp_irq_handler;
125  	void *psp_irq_data;
126  
127  	void *ccp_data;
128  	void *psp_data;
129  };
130  
131  int sp_pci_init(void);
132  void sp_pci_exit(void);
133  
134  int sp_platform_init(void);
135  void sp_platform_exit(void);
136  
137  struct sp_device *sp_alloc_struct(struct device *dev);
138  
139  int sp_init(struct sp_device *sp);
140  void sp_destroy(struct sp_device *sp);
141  
142  int sp_suspend(struct sp_device *sp);
143  int sp_resume(struct sp_device *sp);
144  int sp_request_ccp_irq(struct sp_device *sp, irq_handler_t handler,
145  		       const char *name, void *data);
146  void sp_free_ccp_irq(struct sp_device *sp, void *data);
147  int sp_request_psp_irq(struct sp_device *sp, irq_handler_t handler,
148  		       const char *name, void *data);
149  void sp_free_psp_irq(struct sp_device *sp, void *data);
150  struct sp_device *sp_get_psp_master_device(void);
151  
152  #ifdef CONFIG_CRYPTO_DEV_SP_CCP
153  
154  int ccp_dev_init(struct sp_device *sp);
155  void ccp_dev_destroy(struct sp_device *sp);
156  
157  void ccp_dev_suspend(struct sp_device *sp);
158  void ccp_dev_resume(struct sp_device *sp);
159  
160  #else	/* !CONFIG_CRYPTO_DEV_SP_CCP */
161  
ccp_dev_init(struct sp_device * sp)162  static inline int ccp_dev_init(struct sp_device *sp)
163  {
164  	return 0;
165  }
ccp_dev_destroy(struct sp_device * sp)166  static inline void ccp_dev_destroy(struct sp_device *sp) { }
ccp_dev_suspend(struct sp_device * sp)167  static inline void ccp_dev_suspend(struct sp_device *sp) { }
ccp_dev_resume(struct sp_device * sp)168  static inline void ccp_dev_resume(struct sp_device *sp) { }
169  #endif	/* CONFIG_CRYPTO_DEV_SP_CCP */
170  
171  #ifdef CONFIG_CRYPTO_DEV_SP_PSP
172  
173  int psp_dev_init(struct sp_device *sp);
174  void psp_pci_init(void);
175  void psp_dev_destroy(struct sp_device *sp);
176  void psp_pci_exit(void);
177  
178  #else /* !CONFIG_CRYPTO_DEV_SP_PSP */
179  
psp_dev_init(struct sp_device * sp)180  static inline int psp_dev_init(struct sp_device *sp) { return 0; }
psp_pci_init(void)181  static inline void psp_pci_init(void) { }
psp_dev_destroy(struct sp_device * sp)182  static inline void psp_dev_destroy(struct sp_device *sp) { }
psp_pci_exit(void)183  static inline void psp_pci_exit(void) { }
184  
185  #endif /* CONFIG_CRYPTO_DEV_SP_PSP */
186  
187  #endif
188