GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / fsl-mc / include / mc.h
1 /*
2  * Freescale Management Complex (MC) bus public interface
3  *
4  * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
5  * Author: German Rivera <German.Rivera@freescale.com>
6  *
7  * This file is licensed under the terms of the GNU General Public
8  * License version 2. This program is licensed "as is" without any
9  * warranty of any kind, whether express or implied.
10  */
11 #ifndef _FSL_MC_H_
12 #define _FSL_MC_H_
13
14 #include <linux/device.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/interrupt.h>
17
18 #define FSL_MC_VENDOR_FREESCALE 0x1957
19
20 struct irq_domain;
21 struct msi_domain_info;
22
23 struct fsl_mc_device;
24 struct fsl_mc_io;
25
26 /**
27  * struct fsl_mc_driver - MC object device driver object
28  * @driver: Generic device driver
29  * @match_id_table: table of supported device matching Ids
30  * @probe: Function called when a device is added
31  * @remove: Function called when a device is removed
32  * @shutdown: Function called at shutdown time to quiesce the device
33  * @suspend: Function called when a device is stopped
34  * @resume: Function called when a device is resumed
35  *
36  * Generic DPAA device driver object for device drivers that are registered
37  * with a DPRC bus. This structure is to be embedded in each device-specific
38  * driver structure.
39  */
40 struct fsl_mc_driver {
41         struct device_driver driver;
42         const struct fsl_mc_device_id *match_id_table;
43         int (*probe)(struct fsl_mc_device *dev);
44         int (*remove)(struct fsl_mc_device *dev);
45         void (*shutdown)(struct fsl_mc_device *dev);
46         int (*suspend)(struct fsl_mc_device *dev, pm_message_t state);
47         int (*resume)(struct fsl_mc_device *dev);
48 };
49
50 #define to_fsl_mc_driver(_drv) \
51         container_of(_drv, struct fsl_mc_driver, driver)
52
53 /**
54  * enum fsl_mc_pool_type - Types of allocatable MC bus resources
55  *
56  * Entries in these enum are used as indices in the array of resource
57  * pools of an fsl_mc_bus object.
58  */
59 enum fsl_mc_pool_type {
60         FSL_MC_POOL_DPMCP = 0x0,    /* corresponds to "dpmcp" in the MC */
61         FSL_MC_POOL_DPBP,           /* corresponds to "dpbp" in the MC */
62         FSL_MC_POOL_DPCON,          /* corresponds to "dpcon" in the MC */
63         FSL_MC_POOL_IRQ,
64
65         /*
66          * NOTE: New resource pool types must be added before this entry
67          */
68         FSL_MC_NUM_POOL_TYPES
69 };
70
71 /**
72  * struct fsl_mc_resource - MC generic resource
73  * @type: type of resource
74  * @id: unique MC resource Id within the resources of the same type
75  * @data: pointer to resource-specific data if the resource is currently
76  * allocated, or NULL if the resource is not currently allocated.
77  * @parent_pool: pointer to the parent resource pool from which this
78  * resource is allocated from.
79  * @node: Node in the free list of the corresponding resource pool
80  *
81  * NOTE: This structure is to be embedded as a field of specific
82  * MC resource structures.
83  */
84 struct fsl_mc_resource {
85         enum fsl_mc_pool_type type;
86         s32 id;
87         void *data;
88         struct fsl_mc_resource_pool *parent_pool;
89         struct list_head node;
90 };
91
92 /**
93  * struct fsl_mc_device_irq - MC object device message-based interrupt
94  * @msi_desc: pointer to MSI descriptor allocated by fsl_mc_msi_alloc_descs()
95  * @mc_dev: MC object device that owns this interrupt
96  * @dev_irq_index: device-relative IRQ index
97  * @resource: MC generic resource associated with the interrupt
98  */
99 struct fsl_mc_device_irq {
100         struct msi_desc *msi_desc;
101         struct fsl_mc_device *mc_dev;
102         u8 dev_irq_index;
103         struct fsl_mc_resource resource;
104 };
105
106 #define to_fsl_mc_irq(_mc_resource) \
107         container_of(_mc_resource, struct fsl_mc_device_irq, resource)
108
109 /* Opened state - Indicates that an object is open by at least one owner */
110 #define FSL_MC_OBJ_STATE_OPEN           0x00000001
111 /* Plugged state - Indicates that the object is plugged */
112 #define FSL_MC_OBJ_STATE_PLUGGED        0x00000002
113
114 /**
115  * Shareability flag - Object flag indicating no memory shareability.
116  * the object generates memory accesses that are non coherent with other
117  * masters;
118  * user is responsible for proper memory handling through IOMMU configuration.
119  */
120 #define FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY     0x0001
121
122 /**
123  * struct fsl_mc_obj_desc - Object descriptor
124  * @type: Type of object: NULL terminated string
125  * @id: ID of logical object resource
126  * @vendor: Object vendor identifier
127  * @ver_major: Major version number
128  * @ver_minor:  Minor version number
129  * @irq_count: Number of interrupts supported by the object
130  * @region_count: Number of mappable regions supported by the object
131  * @state: Object state: combination of FSL_MC_OBJ_STATE_ states
132  * @label: Object label: NULL terminated string
133  * @flags: Object's flags
134  */
135 struct fsl_mc_obj_desc {
136         char type[16];
137         int id;
138         u16 vendor;
139         u16 ver_major;
140         u16 ver_minor;
141         u8 irq_count;
142         u8 region_count;
143         u32 state;
144         char label[16];
145         u16 flags;
146 };
147
148 /**
149  * Bit masks for a MC object device (struct fsl_mc_device) flags
150  */
151 #define FSL_MC_IS_DPRC  0x0001
152
153 /**
154  * struct fsl_mc_device - MC object device object
155  * @dev: Linux driver model device object
156  * @dma_mask: Default DMA mask
157  * @flags: MC object device flags
158  * @icid: Isolation context ID for the device
159  * @mc_handle: MC handle for the corresponding MC object opened
160  * @mc_io: Pointer to MC IO object assigned to this device or
161  * NULL if none.
162  * @obj_desc: MC description of the DPAA device
163  * @regions: pointer to array of MMIO region entries
164  * @irqs: pointer to array of pointers to interrupts allocated to this device
165  * @resource: generic resource associated with this MC object device, if any.
166  *
167  * Generic device object for MC object devices that are "attached" to a
168  * MC bus.
169  *
170  * NOTES:
171  * - For a non-DPRC object its icid is the same as its parent DPRC's icid.
172  * - The SMMU notifier callback gets invoked after device_add() has been
173  *   called for an MC object device, but before the device-specific probe
174  *   callback gets called.
175  * - DP_OBJ_DPRC objects are the only MC objects that have built-in MC
176  *   portals. For all other MC objects, their device drivers are responsible for
177  *   allocating MC portals for them by calling fsl_mc_portal_allocate().
178  * - Some types of MC objects (e.g., DP_OBJ_DPBP, DP_OBJ_DPCON) are
179  *   treated as resources that can be allocated/deallocated from the
180  *   corresponding resource pool in the object's parent DPRC, using the
181  *   fsl_mc_object_allocate()/fsl_mc_object_free() functions. These MC objects
182  *   are known as "allocatable" objects. For them, the corresponding
183  *   fsl_mc_device's 'resource' points to the associated resource object.
184  *   For MC objects that are not allocatable (e.g., DP_OBJ_DPRC, DP_OBJ_DPNI),
185  *   'resource' is NULL.
186  */
187 struct fsl_mc_device {
188         struct device dev;
189         u64 dma_mask;
190         u16 flags;
191         u16 icid;
192         u16 mc_handle;
193         struct fsl_mc_io *mc_io;
194         struct fsl_mc_obj_desc obj_desc;
195         struct resource *regions;
196         struct fsl_mc_device_irq **irqs;
197         struct fsl_mc_resource *resource;
198 };
199
200 #define to_fsl_mc_device(_dev) \
201         container_of(_dev, struct fsl_mc_device, dev)
202
203 #define MC_CMD_NUM_OF_PARAMS    7
204
205 struct mc_cmd_header {
206         u8 src_id;
207         u8 flags_hw;
208         u8 status;
209         u8 flags_sw;
210         __le16 token;
211         __le16 cmd_id;
212 };
213
214 struct mc_command {
215         u64 header;
216         u64 params[MC_CMD_NUM_OF_PARAMS];
217 };
218
219 enum mc_cmd_status {
220         MC_CMD_STATUS_OK = 0x0, /* Completed successfully */
221         MC_CMD_STATUS_READY = 0x1, /* Ready to be processed */
222         MC_CMD_STATUS_AUTH_ERR = 0x3, /* Authentication error */
223         MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /* No privilege */
224         MC_CMD_STATUS_DMA_ERR = 0x5, /* DMA or I/O error */
225         MC_CMD_STATUS_CONFIG_ERR = 0x6, /* Configuration error */
226         MC_CMD_STATUS_TIMEOUT = 0x7, /* Operation timed out */
227         MC_CMD_STATUS_NO_RESOURCE = 0x8, /* No resources */
228         MC_CMD_STATUS_NO_MEMORY = 0x9, /* No memory available */
229         MC_CMD_STATUS_BUSY = 0xA, /* Device is busy */
230         MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /* Unsupported operation */
231         MC_CMD_STATUS_INVALID_STATE = 0xC /* Invalid state */
232 };
233
234 /*
235  * MC command flags
236  */
237
238 /* High priority flag */
239 #define MC_CMD_FLAG_PRI         0x80
240 /* Command completion flag */
241 #define MC_CMD_FLAG_INTR_DIS    0x01
242
243 static inline u64 mc_encode_cmd_header(u16 cmd_id,
244                                        u32 cmd_flags,
245                                        u16 token)
246 {
247         u64 header = 0;
248         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&header;
249
250         hdr->cmd_id = cpu_to_le16(cmd_id);
251         hdr->token  = cpu_to_le16(token);
252         hdr->status = MC_CMD_STATUS_READY;
253         if (cmd_flags & MC_CMD_FLAG_PRI)
254                 hdr->flags_hw = MC_CMD_FLAG_PRI;
255         if (cmd_flags & MC_CMD_FLAG_INTR_DIS)
256                 hdr->flags_sw = MC_CMD_FLAG_INTR_DIS;
257
258         return header;
259 }
260
261 static inline u16 mc_cmd_hdr_read_token(struct mc_command *cmd)
262 {
263         struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
264         u16 token = le16_to_cpu(hdr->token);
265
266         return token;
267 }
268
269 struct mc_rsp_create {
270         __le32 object_id;
271 };
272
273 struct mc_rsp_api_ver {
274         __le16 major_ver;
275         __le16 minor_ver;
276 };
277
278 static inline u32 mc_cmd_read_object_id(struct mc_command *cmd)
279 {
280         struct mc_rsp_create *rsp_params;
281
282         rsp_params = (struct mc_rsp_create *)cmd->params;
283         return le32_to_cpu(rsp_params->object_id);
284 }
285
286 static inline void mc_cmd_read_api_version(struct mc_command *cmd,
287                                            u16 *major_ver,
288                                            u16 *minor_ver)
289 {
290         struct mc_rsp_api_ver *rsp_params;
291
292         rsp_params = (struct mc_rsp_api_ver *)cmd->params;
293         *major_ver = le16_to_cpu(rsp_params->major_ver);
294         *minor_ver = le16_to_cpu(rsp_params->minor_ver);
295 }
296
297 /**
298  * Bit masks for a MC I/O object (struct fsl_mc_io) flags
299  */
300 #define FSL_MC_IO_ATOMIC_CONTEXT_PORTAL 0x0001
301
302 /**
303  * struct fsl_mc_io - MC I/O object to be passed-in to mc_send_command()
304  * @dev: device associated with this Mc I/O object
305  * @flags: flags for mc_send_command()
306  * @portal_size: MC command portal size in bytes
307  * @portal_phys_addr: MC command portal physical address
308  * @portal_virt_addr: MC command portal virtual address
309  * @dpmcp_dev: pointer to the DPMCP device associated with the MC portal.
310  *
311  * Fields are only meaningful if the FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is not
312  * set:
313  * @mutex: Mutex to serialize mc_send_command() calls that use the same MC
314  * portal, if the fsl_mc_io object was created with the
315  * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag off. mc_send_command() calls for this
316  * fsl_mc_io object must be made only from non-atomic context.
317  *
318  * Fields are only meaningful if the FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is
319  * set:
320  * @spinlock: Spinlock to serialize mc_send_command() calls that use the same MC
321  * portal, if the fsl_mc_io object was created with the
322  * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag on. mc_send_command() calls for this
323  * fsl_mc_io object can be made from atomic or non-atomic context.
324  */
325 struct fsl_mc_io {
326         struct device *dev;
327         u16 flags;
328         u16 portal_size;
329         phys_addr_t portal_phys_addr;
330         void __iomem *portal_virt_addr;
331         struct fsl_mc_device *dpmcp_dev;
332         union {
333                 /*
334                  * This field is only meaningful if the
335                  * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is not set
336                  */
337                 struct mutex mutex; /* serializes mc_send_command() */
338
339                 /*
340                  * This field is only meaningful if the
341                  * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is set
342                  */
343                 spinlock_t spinlock;    /* serializes mc_send_command() */
344         };
345 };
346
347 int mc_send_command(struct fsl_mc_io *mc_io, struct mc_command *cmd);
348
349 #ifdef CONFIG_FSL_MC_BUS
350 #define dev_is_fsl_mc(_dev) ((_dev)->bus == &fsl_mc_bus_type)
351 #else
352 /* If fsl-mc bus is not present device cannot belong to fsl-mc bus */
353 #define dev_is_fsl_mc(_dev) (0)
354 #endif
355
356 /*
357  * module_fsl_mc_driver() - Helper macro for drivers that don't do
358  * anything special in module init/exit.  This eliminates a lot of
359  * boilerplate.  Each module may only use this macro once, and
360  * calling it replaces module_init() and module_exit()
361  */
362 #define module_fsl_mc_driver(__fsl_mc_driver) \
363         module_driver(__fsl_mc_driver, fsl_mc_driver_register, \
364                       fsl_mc_driver_unregister)
365
366 /*
367  * Macro to avoid include chaining to get THIS_MODULE
368  */
369 #define fsl_mc_driver_register(drv) \
370         __fsl_mc_driver_register(drv, THIS_MODULE)
371
372 int __must_check __fsl_mc_driver_register(struct fsl_mc_driver *fsl_mc_driver,
373                                           struct module *owner);
374
375 void fsl_mc_driver_unregister(struct fsl_mc_driver *driver);
376
377 int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
378                                         u16 mc_io_flags,
379                                         struct fsl_mc_io **new_mc_io);
380
381 void fsl_mc_portal_free(struct fsl_mc_io *mc_io);
382
383 int fsl_mc_portal_reset(struct fsl_mc_io *mc_io);
384
385 int __must_check fsl_mc_object_allocate(struct fsl_mc_device *mc_dev,
386                                         enum fsl_mc_pool_type pool_type,
387                                         struct fsl_mc_device **new_mc_adev);
388
389 void fsl_mc_object_free(struct fsl_mc_device *mc_adev);
390
391 struct irq_domain *fsl_mc_msi_create_irq_domain(struct fwnode_handle *fwnode,
392                                                 struct msi_domain_info *info,
393                                                 struct irq_domain *parent);
394
395 int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev);
396
397 void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev);
398
399 extern struct bus_type fsl_mc_bus_type;
400
401 #endif /* _FSL_MC_H_ */