GNU Linux-libre 4.14.265-gnu1
[releases.git] / drivers / hwtracing / intel_th / intel_th.h
1 /*
2  * Intel(R) Trace Hub data structures
3  *
4  * Copyright (C) 2014-2015 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15
16 #ifndef __INTEL_TH_H__
17 #define __INTEL_TH_H__
18
19 /* intel_th_device device types */
20 enum {
21         /* Devices that generate trace data */
22         INTEL_TH_SOURCE = 0,
23         /* Output ports (MSC, PTI) */
24         INTEL_TH_OUTPUT,
25         /* Switch, the Global Trace Hub (GTH) */
26         INTEL_TH_SWITCH,
27 };
28
29 /**
30  * struct intel_th_output - descriptor INTEL_TH_OUTPUT type devices
31  * @port:       output port number, assigned by the switch
32  * @type:       GTH_{MSU,CTP,PTI}
33  * @scratchpad: scratchpad bits to flag when this output is enabled
34  * @multiblock: true for multiblock output configuration
35  * @active:     true when this output is enabled
36  *
37  * Output port descriptor, used by switch driver to tell which output
38  * port this output device corresponds to. Filled in at output device's
39  * probe time by switch::assign(). Passed from output device driver to
40  * switch related code to enable/disable its port.
41  */
42 struct intel_th_output {
43         int             port;
44         unsigned int    type;
45         unsigned int    scratchpad;
46         bool            multiblock;
47         bool            active;
48 };
49
50 /**
51  * struct intel_th_drvdata - describes hardware capabilities and quirks
52  * @tscu_enable:        device needs SW to enable time stamping unit
53  */
54 struct intel_th_drvdata {
55         unsigned int    tscu_enable        : 1;
56 };
57
58 #define INTEL_TH_CAP(_th, _cap) ((_th)->drvdata ? (_th)->drvdata->_cap : 0)
59
60 /**
61  * struct intel_th_device - device on the intel_th bus
62  * @dev:                device
63  * @drvdata:            hardware capabilities/quirks
64  * @resource:           array of resources available to this device
65  * @num_resources:      number of resources in @resource array
66  * @type:               INTEL_TH_{SOURCE,OUTPUT,SWITCH}
67  * @id:                 device instance or -1
68  * @host_mode:          Intel TH is controlled by an external debug host
69  * @output:             output descriptor for INTEL_TH_OUTPUT devices
70  * @name:               device name to match the driver
71  */
72 struct intel_th_device {
73         struct device           dev;
74         struct intel_th_drvdata *drvdata;
75         struct resource         *resource;
76         unsigned int            num_resources;
77         unsigned int            type;
78         int                     id;
79
80         /* INTEL_TH_SWITCH specific */
81         bool                    host_mode;
82
83         /* INTEL_TH_OUTPUT specific */
84         struct intel_th_output  output;
85
86         char            name[];
87 };
88
89 #define to_intel_th_device(_d)                          \
90         container_of((_d), struct intel_th_device, dev)
91
92 /**
93  * intel_th_device_get_resource() - obtain @num'th resource of type @type
94  * @thdev:      the device to search the resource for
95  * @type:       resource type
96  * @num:        number of the resource
97  */
98 static inline struct resource *
99 intel_th_device_get_resource(struct intel_th_device *thdev, unsigned int type,
100                              unsigned int num)
101 {
102         int i;
103
104         for (i = 0; i < thdev->num_resources; i++)
105                 if (resource_type(&thdev->resource[i]) == type && !num--)
106                         return &thdev->resource[i];
107
108         return NULL;
109 }
110
111 /*
112  * GTH, output ports configuration
113  */
114 enum {
115         GTH_NONE = 0,
116         GTH_MSU,        /* memory/usb */
117         GTH_CTP,        /* Common Trace Port */
118         GTH_LPP,        /* Low Power Path */
119         GTH_PTI,        /* MIPI-PTI */
120 };
121
122 /**
123  * intel_th_output_assigned() - if an output device is assigned to a switch port
124  * @thdev:      the output device
125  *
126  * Return:      true if the device is INTEL_TH_OUTPUT *and* is assigned a port
127  */
128 static inline bool
129 intel_th_output_assigned(struct intel_th_device *thdev)
130 {
131         return thdev->type == INTEL_TH_OUTPUT &&
132                 (thdev->output.port >= 0 ||
133                  thdev->output.type == GTH_NONE);
134 }
135
136 /**
137  * struct intel_th_driver - driver for an intel_th_device device
138  * @driver:     generic driver
139  * @probe:      probe method
140  * @remove:     remove method
141  * @assign:     match a given output type device against available outputs
142  * @unassign:   deassociate an output type device from an output port
143  * @prepare:    prepare output port for tracing
144  * @enable:     enable tracing for a given output device
145  * @disable:    disable tracing for a given output device
146  * @irq:        interrupt callback
147  * @activate:   enable tracing on the output's side
148  * @deactivate: disable tracing on the output's side
149  * @fops:       file operations for device nodes
150  * @attr_group: attributes provided by the driver
151  *
152  * Callbacks @probe and @remove are required for all device types.
153  * Switch device driver needs to fill in @assign, @enable and @disable
154  * callbacks.
155  */
156 struct intel_th_driver {
157         struct device_driver    driver;
158         int                     (*probe)(struct intel_th_device *thdev);
159         void                    (*remove)(struct intel_th_device *thdev);
160         /* switch (GTH) ops */
161         int                     (*assign)(struct intel_th_device *thdev,
162                                           struct intel_th_device *othdev);
163         void                    (*unassign)(struct intel_th_device *thdev,
164                                             struct intel_th_device *othdev);
165         void                    (*prepare)(struct intel_th_device *thdev,
166                                            struct intel_th_output *output);
167         void                    (*enable)(struct intel_th_device *thdev,
168                                           struct intel_th_output *output);
169         void                    (*disable)(struct intel_th_device *thdev,
170                                            struct intel_th_output *output);
171         /* output ops */
172         void                    (*irq)(struct intel_th_device *thdev);
173         int                     (*activate)(struct intel_th_device *thdev);
174         void                    (*deactivate)(struct intel_th_device *thdev);
175         /* file_operations for those who want a device node */
176         const struct file_operations *fops;
177         /* optional attributes */
178         struct attribute_group  *attr_group;
179
180         /* source ops */
181         int                     (*set_output)(struct intel_th_device *thdev,
182                                               unsigned int master);
183 };
184
185 #define to_intel_th_driver(_d)                                  \
186         container_of((_d), struct intel_th_driver, driver)
187
188 #define to_intel_th_driver_or_null(_d)          \
189         ((_d) ? to_intel_th_driver(_d) : NULL)
190
191 /*
192  * Subdevice tree structure is as follows:
193  * + struct intel_th device (pci; dev_{get,set}_drvdata()
194  *   + struct intel_th_device INTEL_TH_SWITCH (GTH)
195  *     + struct intel_th_device INTEL_TH_OUTPUT (MSU, PTI)
196  *   + struct intel_th_device INTEL_TH_SOURCE (STH)
197  *
198  * In other words, INTEL_TH_OUTPUT devices are children of INTEL_TH_SWITCH;
199  * INTEL_TH_SWITCH and INTEL_TH_SOURCE are children of the intel_th device.
200  */
201 static inline struct intel_th_device *
202 to_intel_th_parent(struct intel_th_device *thdev)
203 {
204         struct device *parent = thdev->dev.parent;
205
206         if (!parent)
207                 return NULL;
208
209         return to_intel_th_device(parent);
210 }
211
212 static inline struct intel_th *to_intel_th(struct intel_th_device *thdev)
213 {
214         if (thdev->type == INTEL_TH_OUTPUT)
215                 thdev = to_intel_th_parent(thdev);
216
217         if (WARN_ON_ONCE(!thdev || thdev->type == INTEL_TH_OUTPUT))
218                 return NULL;
219
220         return dev_get_drvdata(thdev->dev.parent);
221 }
222
223 struct intel_th *
224 intel_th_alloc(struct device *dev, struct intel_th_drvdata *drvdata,
225                struct resource *devres, unsigned int ndevres, int irq);
226 void intel_th_free(struct intel_th *th);
227
228 int intel_th_driver_register(struct intel_th_driver *thdrv);
229 void intel_th_driver_unregister(struct intel_th_driver *thdrv);
230
231 int intel_th_trace_enable(struct intel_th_device *thdev);
232 int intel_th_trace_disable(struct intel_th_device *thdev);
233 int intel_th_set_output(struct intel_th_device *thdev,
234                         unsigned int master);
235 int intel_th_output_enable(struct intel_th *th, unsigned int otype);
236
237 enum {
238         TH_MMIO_CONFIG = 0,
239         TH_MMIO_SW = 2,
240         TH_MMIO_END,
241 };
242
243 #define TH_POSSIBLE_OUTPUTS     8
244 /* Total number of possible subdevices: outputs + GTH + STH */
245 #define TH_SUBDEVICE_MAX        (TH_POSSIBLE_OUTPUTS + 2)
246 #define TH_CONFIGURABLE_MASTERS 256
247 #define TH_MSC_MAX              2
248
249 /**
250  * struct intel_th - Intel TH controller
251  * @dev:        driver core's device
252  * @thdev:      subdevices
253  * @hub:        "switch" subdevice (GTH)
254  * @resource:   resources of the entire controller
255  * @num_thdevs: number of devices in the @thdev array
256  * @num_resources:      number or resources in the @resource array
257  * @irq:        irq number
258  * @id:         this Intel TH controller's device ID in the system
259  * @major:      device node major for output devices
260  */
261 struct intel_th {
262         struct device           *dev;
263
264         struct intel_th_device  *thdev[TH_SUBDEVICE_MAX];
265         struct intel_th_device  *hub;
266         struct intel_th_drvdata *drvdata;
267
268         struct resource         *resource;
269         int                     (*activate)(struct intel_th *);
270         void                    (*deactivate)(struct intel_th *);
271         unsigned int            num_thdevs;
272         unsigned int            num_resources;
273         int                     irq;
274
275         int                     id;
276         int                     major;
277 #ifdef CONFIG_MODULES
278         struct work_struct      request_module_work;
279 #endif /* CONFIG_MODULES */
280 #ifdef CONFIG_INTEL_TH_DEBUG
281         struct dentry           *dbg;
282 #endif
283 };
284
285 static inline struct intel_th_device *
286 to_intel_th_hub(struct intel_th_device *thdev)
287 {
288         if (thdev->type == INTEL_TH_SWITCH)
289                 return thdev;
290         else if (thdev->type == INTEL_TH_OUTPUT)
291                 return to_intel_th_parent(thdev);
292
293         return to_intel_th(thdev)->hub;
294 }
295
296 /*
297  * Register windows
298  */
299 enum {
300         /* Global Trace Hub (GTH) */
301         REG_GTH_OFFSET          = 0x0000,
302         REG_GTH_LENGTH          = 0x2000,
303
304         /* Timestamp counter unit (TSCU) */
305         REG_TSCU_OFFSET         = 0x2000,
306         REG_TSCU_LENGTH         = 0x1000,
307
308         /* Software Trace Hub (STH) [0x4000..0x4fff] */
309         REG_STH_OFFSET          = 0x4000,
310         REG_STH_LENGTH          = 0x2000,
311
312         /* Memory Storage Unit (MSU) [0xa0000..0xa1fff] */
313         REG_MSU_OFFSET          = 0xa0000,
314         REG_MSU_LENGTH          = 0x02000,
315
316         /* Internal MSU trace buffer [0x80000..0x9ffff] */
317         BUF_MSU_OFFSET          = 0x80000,
318         BUF_MSU_LENGTH          = 0x20000,
319
320         /* PTI output == same window as GTH */
321         REG_PTI_OFFSET          = REG_GTH_OFFSET,
322         REG_PTI_LENGTH          = REG_GTH_LENGTH,
323
324         /* DCI Handler (DCIH) == some window as MSU */
325         REG_DCIH_OFFSET         = REG_MSU_OFFSET,
326         REG_DCIH_LENGTH         = REG_MSU_LENGTH,
327 };
328
329 /*
330  * Scratchpad bits: tell firmware and external debuggers
331  * what we are up to.
332  */
333 enum {
334         /* Memory is the primary destination */
335         SCRPD_MEM_IS_PRIM_DEST          = BIT(0),
336         /* XHCI DbC is the primary destination */
337         SCRPD_DBC_IS_PRIM_DEST          = BIT(1),
338         /* PTI is the primary destination */
339         SCRPD_PTI_IS_PRIM_DEST          = BIT(2),
340         /* BSSB is the primary destination */
341         SCRPD_BSSB_IS_PRIM_DEST         = BIT(3),
342         /* PTI is the alternate destination */
343         SCRPD_PTI_IS_ALT_DEST           = BIT(4),
344         /* BSSB is the alternate destination */
345         SCRPD_BSSB_IS_ALT_DEST          = BIT(5),
346         /* DeepSx exit occurred */
347         SCRPD_DEEPSX_EXIT               = BIT(6),
348         /* S4 exit occurred */
349         SCRPD_S4_EXIT                   = BIT(7),
350         /* S5 exit occurred */
351         SCRPD_S5_EXIT                   = BIT(8),
352         /* MSU controller 0/1 is enabled */
353         SCRPD_MSC0_IS_ENABLED           = BIT(9),
354         SCRPD_MSC1_IS_ENABLED           = BIT(10),
355         /* Sx exit occurred */
356         SCRPD_SX_EXIT                   = BIT(11),
357         /* Trigger Unit is enabled */
358         SCRPD_TRIGGER_IS_ENABLED        = BIT(12),
359         SCRPD_ODLA_IS_ENABLED           = BIT(13),
360         SCRPD_SOCHAP_IS_ENABLED         = BIT(14),
361         SCRPD_STH_IS_ENABLED            = BIT(15),
362         SCRPD_DCIH_IS_ENABLED           = BIT(16),
363         SCRPD_VER_IS_ENABLED            = BIT(17),
364         /* External debugger is using Intel TH */
365         SCRPD_DEBUGGER_IN_USE           = BIT(24),
366 };
367
368 #endif