blob: e4399e0d66b8a74fe641d583239954b61dfd46f2 [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2002,2007-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#ifndef __KGSL_DEVICE_H
14#define __KGSL_DEVICE_H
15
16#include <linux/slab.h>
17#include <linux/idr.h>
18#include <linux/pm_qos.h>
19#include <linux/sched.h>
20
21#include "kgsl.h"
22#include "kgsl_mmu.h"
23#include "kgsl_pwrctrl.h"
24#include "kgsl_log.h"
25#include "kgsl_pwrscale.h"
26#include "kgsl_snapshot.h"
27#include "kgsl_sharedmem.h"
28#include "kgsl_drawobj.h"
Kyle Pieferb1027b02017-02-10 13:58:58 -080029#include "kgsl_gmu.h"
Shrenuj Bansala419c792016-10-20 14:05:11 -070030
31#define KGSL_IOCTL_FUNC(_cmd, _func) \
32 [_IOC_NR((_cmd))] = \
33 { .cmd = (_cmd), .func = (_func) }
34
35/*
36 * KGSL device state is initialized to INIT when platform_probe *
37 * successfully initialized the device. Once a device has been opened *
38 * (started) it becomes active. NAP implies that only low latency *
39 * resources (for now clocks on some platforms) are off. SLEEP implies *
40 * that the KGSL module believes a device is idle (has been inactive *
41 * past its timer) and all system resources are released. SUSPEND is *
42 * requested by the kernel and will be enforced upon all open devices. *
Kyle Piefere923b7a2017-03-28 17:31:48 -070043 * RESET indicates that GPU or GMU hang happens. KGSL is handling *
44 * snapshot or recover GPU from hang. *
Shrenuj Bansala419c792016-10-20 14:05:11 -070045 */
46
47#define KGSL_STATE_NONE 0x00000000
48#define KGSL_STATE_INIT 0x00000001
49#define KGSL_STATE_ACTIVE 0x00000002
50#define KGSL_STATE_NAP 0x00000004
51#define KGSL_STATE_SUSPEND 0x00000010
52#define KGSL_STATE_AWARE 0x00000020
53#define KGSL_STATE_SLUMBER 0x00000080
Kyle Piefere923b7a2017-03-28 17:31:48 -070054#define KGSL_STATE_RESET 0x00000100
Shrenuj Bansala419c792016-10-20 14:05:11 -070055
56/**
57 * enum kgsl_event_results - result codes passed to an event callback when the
58 * event is retired or cancelled
59 * @KGSL_EVENT_RETIRED: The timestamp associated with the event retired
60 * successflly
61 * @KGSL_EVENT_CANCELLED: The event was cancelled before the event was fired
62 */
63enum kgsl_event_results {
64 KGSL_EVENT_RETIRED = 1,
65 KGSL_EVENT_CANCELLED = 2,
66};
67
68#define KGSL_FLAG_WAKE_ON_TOUCH BIT(0)
69
70/*
71 * "list" of event types for ftrace symbolic magic
72 */
73
74#define KGSL_EVENT_TYPES \
75 { KGSL_EVENT_RETIRED, "retired" }, \
76 { KGSL_EVENT_CANCELLED, "cancelled" }
77
78#define KGSL_CONTEXT_FLAGS \
79 { KGSL_CONTEXT_NO_GMEM_ALLOC, "NO_GMEM_ALLOC" }, \
80 { KGSL_CONTEXT_PREAMBLE, "PREAMBLE" }, \
81 { KGSL_CONTEXT_TRASH_STATE, "TRASH_STATE" }, \
82 { KGSL_CONTEXT_CTX_SWITCH, "CTX_SWITCH" }, \
83 { KGSL_CONTEXT_PER_CONTEXT_TS, "PER_CONTEXT_TS" }, \
84 { KGSL_CONTEXT_USER_GENERATED_TS, "USER_TS" }, \
85 { KGSL_CONTEXT_NO_FAULT_TOLERANCE, "NO_FT" }, \
Hareesh Gunduccfb89b2017-04-14 18:36:20 +053086 { KGSL_CONTEXT_INVALIDATE_ON_FAULT, "INVALIDATE_ON_FAULT" }, \
Shrenuj Bansala419c792016-10-20 14:05:11 -070087 { KGSL_CONTEXT_PWR_CONSTRAINT, "PWR" }, \
Harshdeep Dhatt144c0092017-08-09 10:12:13 -060088 { KGSL_CONTEXT_SAVE_GMEM, "SAVE_GMEM" }, \
89 { KGSL_CONTEXT_IFH_NOP, "IFH_NOP" }, \
90 { KGSL_CONTEXT_SECURE, "SECURE" }, \
91 { KGSL_CONTEXT_NO_SNAPSHOT, "NO_SNAPSHOT" }, \
92 { KGSL_CONTEXT_SPARSE, "SPARSE" }
93
Shrenuj Bansala419c792016-10-20 14:05:11 -070094
95#define KGSL_CONTEXT_TYPES \
96 { KGSL_CONTEXT_TYPE_ANY, "ANY" }, \
97 { KGSL_CONTEXT_TYPE_GL, "GL" }, \
98 { KGSL_CONTEXT_TYPE_CL, "CL" }, \
99 { KGSL_CONTEXT_TYPE_C2D, "C2D" }, \
100 { KGSL_CONTEXT_TYPE_RS, "RS" }
101
102#define KGSL_CONTEXT_ID(_context) \
103 ((_context != NULL) ? (_context)->id : KGSL_MEMSTORE_GLOBAL)
104
105/* Allocate 600K for the snapshot static region*/
106#define KGSL_SNAPSHOT_MEMSIZE (600 * 1024)
107
108struct kgsl_device;
109struct platform_device;
110struct kgsl_device_private;
111struct kgsl_context;
112struct kgsl_power_stats;
113struct kgsl_event;
114struct kgsl_snapshot;
115
116struct kgsl_functable {
117 /* Mandatory functions - these functions must be implemented
118 * by the client device. The driver will not check for a NULL
119 * pointer before calling the hook.
120 */
121 void (*regread)(struct kgsl_device *device,
122 unsigned int offsetwords, unsigned int *value);
123 void (*regwrite)(struct kgsl_device *device,
124 unsigned int offsetwords, unsigned int value);
125 int (*idle)(struct kgsl_device *device);
126 bool (*isidle)(struct kgsl_device *device);
127 int (*suspend_context)(struct kgsl_device *device);
128 int (*init)(struct kgsl_device *device);
129 int (*start)(struct kgsl_device *device, int priority);
130 int (*stop)(struct kgsl_device *device);
Kyle Pieferb1027b02017-02-10 13:58:58 -0800131 void (*gmu_regread)(struct kgsl_device *device,
132 unsigned int offsetwords, unsigned int *value);
133 void (*gmu_regwrite)(struct kgsl_device *device,
134 unsigned int offsetwords, unsigned int value);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700135 int (*getproperty)(struct kgsl_device *device,
136 unsigned int type, void __user *value,
137 size_t sizebytes);
138 int (*getproperty_compat)(struct kgsl_device *device,
139 unsigned int type, void __user *value,
140 size_t sizebytes);
141 int (*waittimestamp)(struct kgsl_device *device,
142 struct kgsl_context *context, unsigned int timestamp,
143 unsigned int msecs);
144 int (*readtimestamp)(struct kgsl_device *device, void *priv,
145 enum kgsl_timestamp_type type, unsigned int *timestamp);
146 int (*queue_cmds)(struct kgsl_device_private *dev_priv,
147 struct kgsl_context *context, struct kgsl_drawobj *drawobj[],
148 uint32_t count, uint32_t *timestamp);
149 void (*power_stats)(struct kgsl_device *device,
150 struct kgsl_power_stats *stats);
151 unsigned int (*gpuid)(struct kgsl_device *device, unsigned int *chipid);
152 void (*snapshot)(struct kgsl_device *device,
153 struct kgsl_snapshot *snapshot, struct kgsl_context *context);
154 irqreturn_t (*irq_handler)(struct kgsl_device *device);
155 int (*drain)(struct kgsl_device *device);
156 /*
157 * Optional functions - these functions are not mandatory. The
158 * driver will check that the function pointer is not NULL before
159 * calling the hook
160 */
161 struct kgsl_context *(*drawctxt_create)(struct kgsl_device_private *,
162 uint32_t *flags);
163 void (*drawctxt_detach)(struct kgsl_context *context);
164 void (*drawctxt_destroy)(struct kgsl_context *context);
165 void (*drawctxt_dump)(struct kgsl_device *device,
166 struct kgsl_context *context);
167 long (*ioctl)(struct kgsl_device_private *dev_priv,
168 unsigned int cmd, unsigned long arg);
169 long (*compat_ioctl)(struct kgsl_device_private *dev_priv,
170 unsigned int cmd, unsigned long arg);
171 int (*setproperty)(struct kgsl_device_private *dev_priv,
172 unsigned int type, void __user *value,
173 unsigned int sizebytes);
174 int (*setproperty_compat)(struct kgsl_device_private *dev_priv,
175 unsigned int type, void __user *value,
176 unsigned int sizebytes);
177 void (*drawctxt_sched)(struct kgsl_device *device,
178 struct kgsl_context *context);
179 void (*resume)(struct kgsl_device *device);
180 int (*regulator_enable)(struct kgsl_device *);
181 bool (*is_hw_collapsible)(struct kgsl_device *);
182 void (*regulator_disable)(struct kgsl_device *);
183 void (*pwrlevel_change_settings)(struct kgsl_device *device,
184 unsigned int prelevel, unsigned int postlevel, bool post);
185 void (*regulator_disable_poll)(struct kgsl_device *device);
186 void (*clk_set_options)(struct kgsl_device *device,
Deepak Kumara309e0e2017-03-17 17:27:42 +0530187 const char *name, struct clk *clk, bool on);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700188 void (*gpu_model)(struct kgsl_device *device, char *str,
189 size_t bufsz);
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +0530190 void (*stop_fault_timer)(struct kgsl_device *device);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700191};
192
193struct kgsl_ioctl {
194 unsigned int cmd;
195 long (*func)(struct kgsl_device_private *, unsigned int, void *);
196};
197
198long kgsl_ioctl_helper(struct file *filep, unsigned int cmd, unsigned long arg,
199 const struct kgsl_ioctl *cmds, int len);
200
201/* Flag to mark the memobj_node as a preamble */
202#define MEMOBJ_PREAMBLE BIT(0)
203/* Flag to mark that the memobj_node should not go to the hadrware */
204#define MEMOBJ_SKIP BIT(1)
205
206/**
207 * struct kgsl_memobj_node - Memory object descriptor
208 * @node: Local list node for the object
209 * @id: GPU memory ID for the object
210 * offset: Offset within the object
211 * @gpuaddr: GPU address for the object
212 * @flags: External flags passed by the user
213 * @priv: Internal flags set by the driver
214 */
215struct kgsl_memobj_node {
216 struct list_head node;
217 unsigned int id;
218 uint64_t offset;
219 uint64_t gpuaddr;
220 uint64_t size;
221 unsigned long flags;
222 unsigned long priv;
223};
224
Tarun Karra2b8b3632016-11-14 16:38:27 -0800225/**
226 * struct kgsl_sparseobj_node - Sparse object descriptor
227 * @node: Local list node for the sparse cmdbatch
228 * @virt_id: Virtual ID to bind/unbind
229 * @obj: struct kgsl_sparse_binding_object
230 */
231struct kgsl_sparseobj_node {
232 struct list_head node;
233 unsigned int virt_id;
234 struct kgsl_sparse_binding_object obj;
235};
236
Shrenuj Bansala419c792016-10-20 14:05:11 -0700237struct kgsl_device {
238 struct device *dev;
239 const char *name;
240 unsigned int ver_major;
241 unsigned int ver_minor;
242 uint32_t flags;
243 enum kgsl_deviceid id;
244
245 /* Starting physical address for GPU registers */
246 unsigned long reg_phys;
247
248 /* Starting Kernel virtual address for GPU registers */
249 void __iomem *reg_virt;
250
251 /* Total memory size for all GPU registers */
252 unsigned int reg_len;
253
254 /* Kernel virtual address for GPU shader memory */
255 void __iomem *shader_mem_virt;
256
257 /* Starting physical address for GPU shader memory */
258 unsigned long shader_mem_phys;
259
260 /* GPU shader memory size */
261 unsigned int shader_mem_len;
262 struct kgsl_memdesc memstore;
263 struct kgsl_memdesc scratch;
264 const char *iomemname;
265 const char *shadermemname;
266
267 struct kgsl_mmu mmu;
Kyle Pieferb1027b02017-02-10 13:58:58 -0800268 struct gmu_device gmu;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700269 struct completion hwaccess_gate;
270 struct completion halt_gate;
271 const struct kgsl_functable *ftbl;
272 struct work_struct idle_check_ws;
273 struct timer_list idle_timer;
274 struct kgsl_pwrctrl pwrctrl;
275 int open_count;
276
Hareesh Gundu2eb74d72017-06-07 14:50:15 +0530277 /* For GPU inline submission */
278 uint32_t submit_now;
279 spinlock_t submit_lock;
280 bool slumber;
281
Shrenuj Bansala419c792016-10-20 14:05:11 -0700282 struct mutex mutex;
283 uint32_t state;
284 uint32_t requested_state;
285
286 atomic_t active_cnt;
287
288 wait_queue_head_t wait_queue;
289 wait_queue_head_t active_cnt_wq;
290 struct platform_device *pdev;
291 struct dentry *d_debugfs;
292 struct idr context_idr;
293 rwlock_t context_lock;
294
295 struct {
296 void *ptr;
297 size_t size;
298 } snapshot_memory;
299
300 struct kgsl_snapshot *snapshot;
301
302 u32 snapshot_faultcount; /* Total number of faults since boot */
303 bool force_panic; /* Force panic after snapshot dump */
304
305 /* Use CP Crash dumper to get GPU snapshot*/
306 bool snapshot_crashdumper;
Harshdeep Dhatt134f7af2017-05-17 13:54:41 -0600307 /* Use HOST side register reads to get GPU snapshot*/
308 bool snapshot_legacy;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700309
310 struct kobject snapshot_kobj;
311
312 struct kobject ppd_kobj;
313
314 /* Logging levels */
315 int cmd_log;
316 int ctxt_log;
317 int drv_log;
318 int mem_log;
319 int pwr_log;
320 struct kgsl_pwrscale pwrscale;
321
322 int reset_counter; /* Track how many GPU core resets have occurred */
323 struct workqueue_struct *events_wq;
324
325 struct device *busmondev; /* pseudo dev for GPU BW voting governor */
326
327 /* Number of active contexts seen globally for this device */
328 int active_context_count;
329 struct kobject *gpu_sysfs_kobj;
330};
331
332#define KGSL_MMU_DEVICE(_mmu) \
333 container_of((_mmu), struct kgsl_device, mmu)
334
335#define KGSL_DEVICE_COMMON_INIT(_dev) \
336 .hwaccess_gate = COMPLETION_INITIALIZER((_dev).hwaccess_gate),\
337 .halt_gate = COMPLETION_INITIALIZER((_dev).halt_gate),\
338 .idle_check_ws = __WORK_INITIALIZER((_dev).idle_check_ws,\
339 kgsl_idle_check),\
340 .context_idr = IDR_INIT((_dev).context_idr),\
341 .wait_queue = __WAIT_QUEUE_HEAD_INITIALIZER((_dev).wait_queue),\
342 .active_cnt_wq = __WAIT_QUEUE_HEAD_INITIALIZER((_dev).active_cnt_wq),\
343 .mutex = __MUTEX_INITIALIZER((_dev).mutex),\
344 .state = KGSL_STATE_NONE,\
345 .ver_major = DRIVER_VERSION_MAJOR,\
346 .ver_minor = DRIVER_VERSION_MINOR
347
348
349/**
350 * enum bits for struct kgsl_context.priv
Hareesh Gundu7d5a8f22017-02-21 13:23:46 +0530351 * @KGSL_CONTEXT_PRIV_SUBMITTED - The context has submitted commands to gpu.
Shrenuj Bansala419c792016-10-20 14:05:11 -0700352 * @KGSL_CONTEXT_PRIV_DETACHED - The context has been destroyed by userspace
353 * and is no longer using the gpu.
354 * @KGSL_CONTEXT_PRIV_INVALID - The context has been destroyed by the kernel
355 * because it caused a GPU fault.
356 * @KGSL_CONTEXT_PRIV_PAGEFAULT - The context has caused a page fault.
357 * @KGSL_CONTEXT_PRIV_DEVICE_SPECIFIC - this value and higher values are
358 * reserved for devices specific use.
359 */
360enum kgsl_context_priv {
Hareesh Gundu7d5a8f22017-02-21 13:23:46 +0530361 KGSL_CONTEXT_PRIV_SUBMITTED = 0,
362 KGSL_CONTEXT_PRIV_DETACHED,
Shrenuj Bansala419c792016-10-20 14:05:11 -0700363 KGSL_CONTEXT_PRIV_INVALID,
364 KGSL_CONTEXT_PRIV_PAGEFAULT,
365 KGSL_CONTEXT_PRIV_DEVICE_SPECIFIC = 16,
366};
367
368struct kgsl_process_private;
369
370/**
371 * struct kgsl_context - The context fields that are valid for a user defined
372 * context
373 * @refcount: kref object for reference counting the context
374 * @id: integer identifier for the context
375 * @priority; The context's priority to submit commands to GPU
376 * @tid: task that created this context.
377 * @dev_priv: pointer to the owning device instance
378 * @proc_priv: pointer to process private, the process that allocated the
379 * context
380 * @priv: in-kernel context flags, use KGSL_CONTEXT_* values
381 * @reset_status: status indication whether a gpu reset occurred and whether
382 * this context was responsible for causing it
383 * @timeline: sync timeline used to create fences that can be signaled when a
384 * sync_pt timestamp expires
385 * @events: A kgsl_event_group for this context - contains the list of GPU
386 * events
387 * @flags: flags from userspace controlling the behavior of this context
388 * @pwr_constraint: power constraint from userspace for this context
389 * @fault_count: number of times gpu hanged in last _context_throttle_time ms
390 * @fault_time: time of the first gpu hang in last _context_throttle_time ms
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -0600391 * @user_ctxt_record: memory descriptor used by CP to save/restore VPC data
392 * across preemption
Shrenuj Bansala419c792016-10-20 14:05:11 -0700393 */
394struct kgsl_context {
395 struct kref refcount;
396 uint32_t id;
397 uint32_t priority;
398 pid_t tid;
399 struct kgsl_device_private *dev_priv;
400 struct kgsl_process_private *proc_priv;
401 unsigned long priv;
402 struct kgsl_device *device;
403 unsigned int reset_status;
Lynus Vazc031a9b2017-01-25 13:00:13 +0530404 struct kgsl_sync_timeline *ktimeline;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700405 struct kgsl_event_group events;
406 unsigned int flags;
407 struct kgsl_pwr_constraint pwr_constraint;
408 unsigned int fault_count;
409 unsigned long fault_time;
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -0600410 struct kgsl_mem_entry *user_ctxt_record;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700411};
412
413#define _context_comm(_c) \
414 (((_c) && (_c)->proc_priv) ? (_c)->proc_priv->comm : "unknown")
415
416/*
417 * Print log messages with the context process name/pid:
418 * [...] kgsl kgsl-3d0: kgsl-api-test[22182]:
419 */
420
421#define pr_context(_d, _c, fmt, args...) \
422 dev_err((_d)->dev, "%s[%d]: " fmt, \
423 _context_comm((_c)), \
424 (_c)->proc_priv->pid, ##args)
425
426/**
427 * struct kgsl_process_private - Private structure for a KGSL process (across
428 * all devices)
429 * @priv: Internal flags, use KGSL_PROCESS_* values
430 * @pid: ID for the task owner of the process
431 * @comm: task name of the process
432 * @mem_lock: Spinlock to protect the process memory lists
433 * @refcount: kref object for reference counting the process
434 * @idr: Iterator for assigning IDs to memory allocations
435 * @pagetable: Pointer to the pagetable owned by this process
436 * @kobj: Pointer to a kobj for the sysfs directory for this process
437 * @debug_root: Pointer to the debugfs root for this process
438 * @stats: Memory allocation statistics for this process
439 * @syncsource_idr: sync sources created by this process
440 * @syncsource_lock: Spinlock to protect the syncsource idr
441 * @fd_count: Counter for the number of FDs for this process
442 */
443struct kgsl_process_private {
444 unsigned long priv;
445 pid_t pid;
446 char comm[TASK_COMM_LEN];
447 spinlock_t mem_lock;
448 struct kref refcount;
449 struct idr mem_idr;
450 struct kgsl_pagetable *pagetable;
451 struct list_head list;
452 struct kobject kobj;
453 struct dentry *debug_root;
454 struct {
455 uint64_t cur;
456 uint64_t max;
457 } stats[KGSL_MEM_ENTRY_MAX];
458 struct idr syncsource_idr;
459 spinlock_t syncsource_lock;
460 int fd_count;
461};
462
463/**
464 * enum kgsl_process_priv_flags - Private flags for kgsl_process_private
465 * @KGSL_PROCESS_INIT: Set if the process structure has been set up
466 */
467enum kgsl_process_priv_flags {
468 KGSL_PROCESS_INIT = 0,
469};
470
471struct kgsl_device_private {
472 struct kgsl_device *device;
473 struct kgsl_process_private *process_priv;
474};
475
476/**
477 * struct kgsl_snapshot - details for a specific snapshot instance
478 * @ib1base: Active IB1 base address at the time of fault
479 * @ib2base: Active IB2 base address at the time of fault
480 * @ib1size: Number of DWORDS pending in IB1 at the time of fault
481 * @ib2size: Number of DWORDS pending in IB2 at the time of fault
482 * @ib1dumped: Active IB1 dump status to sansphot binary
483 * @ib2dumped: Active IB2 dump status to sansphot binary
484 * @start: Pointer to the start of the static snapshot region
485 * @size: Size of the current snapshot instance
486 * @ptr: Pointer to the next block of memory to write to during snapshotting
487 * @remain: Bytes left in the snapshot region
488 * @timestamp: Timestamp of the snapshot instance (in seconds since boot)
489 * @mempool: Pointer to the memory pool for storing memory objects
490 * @mempool_size: Size of the memory pool
491 * @obj_list: List of frozen GPU buffers that are waiting to be dumped.
492 * @cp_list: List of IB's to be dumped.
493 * @work: worker to dump the frozen memory
494 * @dump_gate: completion gate signaled by worker when it is finished.
495 * @process: the process that caused the hang, if known.
496 * @sysfs_read: An atomic for concurrent snapshot reads via syfs.
497 */
498struct kgsl_snapshot {
499 uint64_t ib1base;
500 uint64_t ib2base;
501 unsigned int ib1size;
502 unsigned int ib2size;
503 bool ib1dumped;
504 bool ib2dumped;
505 u8 *start;
506 size_t size;
507 u8 *ptr;
508 size_t remain;
509 unsigned long timestamp;
510 u8 *mempool;
511 size_t mempool_size;
512 struct list_head obj_list;
513 struct list_head cp_list;
514 struct work_struct work;
515 struct completion dump_gate;
516 struct kgsl_process_private *process;
517 atomic_t sysfs_read;
518};
519
520/**
521 * struct kgsl_snapshot_object - GPU memory in the snapshot
522 * @gpuaddr: The GPU address identified during snapshot
523 * @size: The buffer size identified during snapshot
524 * @offset: offset from start of the allocated kgsl_mem_entry
525 * @type: SNAPSHOT_OBJ_TYPE_* identifier.
526 * @entry: the reference counted memory entry for this buffer
527 * @node: node for kgsl_snapshot.obj_list
528 */
529struct kgsl_snapshot_object {
530 uint64_t gpuaddr;
531 uint64_t size;
532 uint64_t offset;
533 int type;
534 struct kgsl_mem_entry *entry;
535 struct list_head node;
536};
537
538struct kgsl_device *kgsl_get_device(int dev_idx);
539
540static inline void kgsl_process_add_stats(struct kgsl_process_private *priv,
541 unsigned int type, uint64_t size)
542{
543 priv->stats[type].cur += size;
544 if (priv->stats[type].max < priv->stats[type].cur)
545 priv->stats[type].max = priv->stats[type].cur;
546}
547
Lynus Vazd37f1d82017-05-24 16:39:15 +0530548static inline bool kgsl_is_register_offset(struct kgsl_device *device,
549 unsigned int offsetwords)
550{
551 return ((offsetwords * sizeof(uint32_t)) < device->reg_len);
552}
553
554static inline bool kgsl_is_gmu_offset(struct kgsl_device *device,
555 unsigned int offsetwords)
556{
557 struct gmu_device *gmu = &device->gmu;
558
559 return (gmu->pdev &&
560 (offsetwords >= gmu->gmu2gpu_offset) &&
561 ((offsetwords - gmu->gmu2gpu_offset) * sizeof(uint32_t) <
562 gmu->reg_len));
563}
564
Shrenuj Bansala419c792016-10-20 14:05:11 -0700565static inline void kgsl_regread(struct kgsl_device *device,
566 unsigned int offsetwords,
567 unsigned int *value)
568{
Lynus Vazd37f1d82017-05-24 16:39:15 +0530569 if (kgsl_is_register_offset(device, offsetwords))
570 device->ftbl->regread(device, offsetwords, value);
571 else if (device->ftbl->gmu_regread &&
572 kgsl_is_gmu_offset(device, offsetwords))
573 device->ftbl->gmu_regread(device, offsetwords, value);
574 else {
575 WARN(1, "Out of bounds register read: 0x%x\n", offsetwords);
576 *value = 0;
577 }
Shrenuj Bansala419c792016-10-20 14:05:11 -0700578}
579
580static inline void kgsl_regwrite(struct kgsl_device *device,
581 unsigned int offsetwords,
582 unsigned int value)
583{
Lynus Vazd37f1d82017-05-24 16:39:15 +0530584 if (kgsl_is_register_offset(device, offsetwords))
585 device->ftbl->regwrite(device, offsetwords, value);
586 else if (device->ftbl->gmu_regwrite &&
587 kgsl_is_gmu_offset(device, offsetwords))
588 device->ftbl->gmu_regwrite(device, offsetwords, value);
589 else
590 WARN(1, "Out of bounds register write: 0x%x\n", offsetwords);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700591}
592
Kyle Pieferb1027b02017-02-10 13:58:58 -0800593static inline void kgsl_gmu_regread(struct kgsl_device *device,
594 unsigned int offsetwords,
595 unsigned int *value)
596{
597 if (device->ftbl->gmu_regread)
598 device->ftbl->gmu_regread(device, offsetwords, value);
599 else
Carter Cooper83454bf2017-03-20 11:26:04 -0600600 *value = 0;
Kyle Pieferb1027b02017-02-10 13:58:58 -0800601}
602
603static inline void kgsl_gmu_regwrite(struct kgsl_device *device,
604 unsigned int offsetwords,
605 unsigned int value)
606{
607 if (device->ftbl->gmu_regwrite)
608 device->ftbl->gmu_regwrite(device, offsetwords, value);
609}
610
Shrenuj Bansala419c792016-10-20 14:05:11 -0700611static inline void kgsl_regrmw(struct kgsl_device *device,
612 unsigned int offsetwords,
613 unsigned int mask, unsigned int bits)
614{
615 unsigned int val = 0;
616
Lynus Vazd37f1d82017-05-24 16:39:15 +0530617 kgsl_regread(device, offsetwords, &val);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700618 val &= ~mask;
Lynus Vazd37f1d82017-05-24 16:39:15 +0530619 kgsl_regwrite(device, offsetwords, val | bits);
Shrenuj Bansala419c792016-10-20 14:05:11 -0700620}
621
Kyle Pieferdc0706c2017-04-13 13:17:50 -0700622static inline void kgsl_gmu_regrmw(struct kgsl_device *device,
623 unsigned int offsetwords,
624 unsigned int mask, unsigned int bits)
625{
626 unsigned int val = 0;
627
628 kgsl_gmu_regread(device, offsetwords, &val);
629 val &= ~mask;
630 kgsl_gmu_regwrite(device, offsetwords, val | bits);
631}
632
Shrenuj Bansala419c792016-10-20 14:05:11 -0700633static inline int kgsl_idle(struct kgsl_device *device)
634{
635 return device->ftbl->idle(device);
636}
637
638static inline unsigned int kgsl_gpuid(struct kgsl_device *device,
639 unsigned int *chipid)
640{
641 return device->ftbl->gpuid(device, chipid);
642}
643
644static inline int kgsl_create_device_sysfs_files(struct device *root,
645 const struct device_attribute **list)
646{
647 int ret = 0, i;
648
649 for (i = 0; list[i] != NULL; i++)
650 ret |= device_create_file(root, list[i]);
651 return ret;
652}
653
654static inline void kgsl_remove_device_sysfs_files(struct device *root,
655 const struct device_attribute **list)
656{
657 int i;
658
659 for (i = 0; list[i] != NULL; i++)
660 device_remove_file(root, list[i]);
661}
662
663static inline struct kgsl_device *kgsl_device_from_dev(struct device *dev)
664{
665 int i;
666
667 for (i = 0; i < KGSL_DEVICE_MAX; i++) {
668 if (kgsl_driver.devp[i] && kgsl_driver.devp[i]->dev == dev)
669 return kgsl_driver.devp[i];
670 }
671
672 return NULL;
673}
674
675static inline int kgsl_state_is_awake(struct kgsl_device *device)
676{
677 if (device->state == KGSL_STATE_ACTIVE ||
678 device->state == KGSL_STATE_AWARE)
679 return true;
680 else
681 return false;
682}
683
684int kgsl_readtimestamp(struct kgsl_device *device, void *priv,
685 enum kgsl_timestamp_type type, unsigned int *timestamp);
686
687int kgsl_check_timestamp(struct kgsl_device *device,
688 struct kgsl_context *context, unsigned int timestamp);
689
690int kgsl_device_platform_probe(struct kgsl_device *device);
691
692void kgsl_device_platform_remove(struct kgsl_device *device);
693
694const char *kgsl_pwrstate_to_str(unsigned int state);
695
696int kgsl_device_snapshot_init(struct kgsl_device *device);
697void kgsl_device_snapshot(struct kgsl_device *device,
698 struct kgsl_context *context);
699void kgsl_device_snapshot_close(struct kgsl_device *device);
700void kgsl_snapshot_save_frozen_objs(struct work_struct *work);
701
702void kgsl_events_init(void);
703void kgsl_events_exit(void);
704
Harshdeep Dhatt2e42f122017-05-31 17:27:19 -0600705void kgsl_context_detach(struct kgsl_context *context);
706
Shrenuj Bansala419c792016-10-20 14:05:11 -0700707void kgsl_del_event_group(struct kgsl_event_group *group);
708
709void kgsl_add_event_group(struct kgsl_event_group *group,
710 struct kgsl_context *context, const char *name,
711 readtimestamp_func readtimestamp, void *priv);
712
713void kgsl_cancel_events_timestamp(struct kgsl_device *device,
714 struct kgsl_event_group *group, unsigned int timestamp);
715void kgsl_cancel_events(struct kgsl_device *device,
716 struct kgsl_event_group *group);
717void kgsl_cancel_event(struct kgsl_device *device,
718 struct kgsl_event_group *group, unsigned int timestamp,
719 kgsl_event_func func, void *priv);
720bool kgsl_event_pending(struct kgsl_device *device,
721 struct kgsl_event_group *group, unsigned int timestamp,
722 kgsl_event_func func, void *priv);
723int kgsl_add_event(struct kgsl_device *device, struct kgsl_event_group *group,
724 unsigned int timestamp, kgsl_event_func func, void *priv);
725void kgsl_process_event_group(struct kgsl_device *device,
726 struct kgsl_event_group *group);
727void kgsl_flush_event_group(struct kgsl_device *device,
728 struct kgsl_event_group *group);
729void kgsl_process_event_groups(struct kgsl_device *device);
730
731void kgsl_context_destroy(struct kref *kref);
732
733int kgsl_context_init(struct kgsl_device_private *dev_priv,
734 struct kgsl_context *context);
735
736void kgsl_context_dump(struct kgsl_context *context);
737
738int kgsl_memfree_find_entry(pid_t ptname, uint64_t *gpuaddr,
739 uint64_t *size, uint64_t *flags, pid_t *pid);
740
741long kgsl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
742
743long kgsl_ioctl_copy_in(unsigned int kernel_cmd, unsigned int user_cmd,
744 unsigned long arg, unsigned char *ptr);
745
746long kgsl_ioctl_copy_out(unsigned int kernel_cmd, unsigned int user_cmd,
747 unsigned long arg, unsigned char *ptr);
748
Tarun Karra2b8b3632016-11-14 16:38:27 -0800749void kgsl_sparse_bind(struct kgsl_process_private *private,
750 struct kgsl_drawobj_sparse *sparse);
751
Shrenuj Bansala419c792016-10-20 14:05:11 -0700752/**
753 * kgsl_context_put() - Release context reference count
754 * @context: Pointer to the KGSL context to be released
755 *
756 * Reduce the reference count on a KGSL context and destroy it if it is no
757 * longer needed
758 */
759static inline void
760kgsl_context_put(struct kgsl_context *context)
761{
762 if (context)
763 kref_put(&context->refcount, kgsl_context_destroy);
764}
765
766/**
767 * kgsl_context_detached() - check if a context is detached
768 * @context: the context
769 *
770 * Check if a context has been destroyed by userspace and is only waiting
771 * for reference counts to go away. This check is used to weed out
772 * contexts that shouldn't use the gpu so NULL is considered detached.
773 */
774static inline bool kgsl_context_detached(struct kgsl_context *context)
775{
776 return (context == NULL || test_bit(KGSL_CONTEXT_PRIV_DETACHED,
777 &context->priv));
778}
779
780/**
781 * kgsl_context_invalid() - check if a context is invalid
782 * @context: the context
783 *
784 * Check if a context has been invalidated by the kernel and may no
785 * longer use the GPU.
786 */
787static inline bool kgsl_context_invalid(struct kgsl_context *context)
788{
789 return (context == NULL || test_bit(KGSL_CONTEXT_PRIV_INVALID,
790 &context->priv));
791}
792
793
794/**
795 * kgsl_context_get() - get a pointer to a KGSL context
796 * @device: Pointer to the KGSL device that owns the context
797 * @id: Context ID
798 *
799 * Find the context associated with the given ID number, increase the reference
800 * count on it and return it. The caller must make sure that this call is
801 * paired with a kgsl_context_put. This function is for internal use because it
802 * doesn't validate the ownership of the context with the calling process - use
803 * kgsl_context_get_owner for that
804 */
805static inline struct kgsl_context *kgsl_context_get(struct kgsl_device *device,
806 uint32_t id)
807{
808 int result = 0;
809 struct kgsl_context *context = NULL;
810
811 read_lock(&device->context_lock);
812
813 context = idr_find(&device->context_idr, id);
814
815 /* Don't return a context that has been detached */
816 if (kgsl_context_detached(context))
817 context = NULL;
818 else
819 result = kref_get_unless_zero(&context->refcount);
820
821 read_unlock(&device->context_lock);
822
823 if (!result)
824 return NULL;
825 return context;
826}
827
828/**
829 * _kgsl_context_get() - lightweight function to just increment the ref count
830 * @context: Pointer to the KGSL context
831 *
832 * Get a reference to the specified KGSL context structure. This is a
833 * lightweight way to just increase the refcount on a known context rather than
834 * walking through kgsl_context_get and searching the iterator
835 */
836static inline int _kgsl_context_get(struct kgsl_context *context)
837{
838 int ret = 0;
839
840 if (context)
841 ret = kref_get_unless_zero(&context->refcount);
842
843 return ret;
844}
845
846/**
847 * kgsl_context_get_owner() - get a pointer to a KGSL context in a specific
848 * process
849 * @dev_priv: Pointer to the process struct
850 * @id: Context ID to return
851 *
852 * Find the context associated with the given ID number, increase the reference
853 * count on it and return it. The caller must make sure that this call is
854 * paired with a kgsl_context_put. This function validates that the context id
855 * given is owned by the dev_priv instancet that is passed in. See
856 * kgsl_context_get for the internal version that doesn't do the check
857 */
858static inline struct kgsl_context *kgsl_context_get_owner(
859 struct kgsl_device_private *dev_priv, uint32_t id)
860{
861 struct kgsl_context *context;
862
863 context = kgsl_context_get(dev_priv->device, id);
864
865 /* Verify that the context belongs to current calling fd. */
866 if (context != NULL && context->dev_priv != dev_priv) {
867 kgsl_context_put(context);
868 return NULL;
869 }
870
871 return context;
872}
873
874/**
875 * kgsl_process_private_get() - increment the refcount on a
876 * kgsl_process_private struct
877 * @process: Pointer to the KGSL process_private
878 *
879 * Returns 0 if the structure is invalid and a reference count could not be
880 * obtained, nonzero otherwise.
881 */
882static inline int kgsl_process_private_get(struct kgsl_process_private *process)
883{
884 int ret = 0;
885
886 if (process != NULL)
887 ret = kref_get_unless_zero(&process->refcount);
888 return ret;
889}
890
891void kgsl_process_private_put(struct kgsl_process_private *private);
892
893
894struct kgsl_process_private *kgsl_process_private_find(pid_t pid);
895
896/**
897 * kgsl_property_read_u32() - Read a u32 property from the device tree
898 * @device: Pointer to the KGSL device
899 * @prop: String name of the property to query
900 * @ptr: Pointer to the variable to store the property
901 */
902static inline int kgsl_property_read_u32(struct kgsl_device *device,
903 const char *prop, unsigned int *ptr)
904{
905 return of_property_read_u32(device->pdev->dev.of_node, prop, ptr);
906}
907
908/**
909 * kgsl_sysfs_store() - parse a string from a sysfs store function
910 * @buf: Incoming string to parse
911 * @ptr: Pointer to an unsigned int to store the value
912 */
913static inline int kgsl_sysfs_store(const char *buf, unsigned int *ptr)
914{
915 unsigned int val;
916 int rc;
917
918 rc = kstrtou32(buf, 0, &val);
919 if (rc)
920 return rc;
921
922 if (ptr)
923 *ptr = val;
924
925 return 0;
926}
927
928/*
929 * A helper macro to print out "not enough memory functions" - this
930 * makes it easy to standardize the messages as well as cut down on
931 * the number of strings in the binary
932 */
933#define SNAPSHOT_ERR_NOMEM(_d, _s) \
934 KGSL_DRV_ERR((_d), \
935 "snapshot: not enough snapshot memory for section %s\n", (_s))
936
937/**
938 * struct kgsl_snapshot_registers - list of registers to snapshot
939 * @regs: Pointer to an array of register ranges
940 * @count: Number of entries in the array
941 */
942struct kgsl_snapshot_registers {
943 const unsigned int *regs;
944 unsigned int count;
945};
946
947size_t kgsl_snapshot_dump_registers(struct kgsl_device *device, u8 *buf,
948 size_t remain, void *priv);
949
950void kgsl_snapshot_indexed_registers(struct kgsl_device *device,
951 struct kgsl_snapshot *snapshot, unsigned int index,
952 unsigned int data, unsigned int start, unsigned int count);
953
954int kgsl_snapshot_get_object(struct kgsl_snapshot *snapshot,
955 struct kgsl_process_private *process, uint64_t gpuaddr,
956 uint64_t size, unsigned int type);
957
958int kgsl_snapshot_have_object(struct kgsl_snapshot *snapshot,
959 struct kgsl_process_private *process,
960 uint64_t gpuaddr, uint64_t size);
961
962struct adreno_ib_object_list;
963
964int kgsl_snapshot_add_ib_obj_list(struct kgsl_snapshot *snapshot,
965 struct adreno_ib_object_list *ib_obj_list);
966
967void kgsl_snapshot_add_section(struct kgsl_device *device, u16 id,
968 struct kgsl_snapshot *snapshot,
969 size_t (*func)(struct kgsl_device *, u8 *, size_t, void *),
970 void *priv);
971
972/**
973 * struct kgsl_pwr_limit - limit structure for each client
974 * @node: Local list node for the limits list
975 * @level: requested power level
976 * @device: pointer to the device structure
977 */
978struct kgsl_pwr_limit {
979 struct list_head node;
980 unsigned int level;
981 struct kgsl_device *device;
982};
983
984#endif /* __KGSL_DEVICE_H */