blob: 942621eb75c4d5533a984f1e7b33c3d675acbb53 [file] [log] [blame]
Sunil Khatrifa43f4a2017-12-12 17:20:53 +05301/* Copyright (c) 2002,2007-2018, The Linux Foundation. All rights reserved.
Shrenuj Bansala419c792016-10-20 14:05:11 -07002 *
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#include <linux/module.h>
14#include <linux/uaccess.h>
15#include <linux/sched.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
18#include <linux/delay.h>
19#include <linux/input.h>
Lynus Vaz9ed8cf92017-09-21 21:55:34 +053020#include <linux/io.h>
Shrenuj Bansala419c792016-10-20 14:05:11 -070021#include <soc/qcom/scm.h>
Deepak Kumar65613682018-01-03 14:55:00 +053022#include <linux/nvmem-consumer.h>
Shrenuj Bansala419c792016-10-20 14:05:11 -070023
24#include <linux/msm-bus-board.h>
25#include <linux/msm-bus.h>
26
27#include "kgsl.h"
28#include "kgsl_pwrscale.h"
29#include "kgsl_sharedmem.h"
30#include "kgsl_iommu.h"
31#include "kgsl_trace.h"
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -060032#include "adreno_llc.h"
Shrenuj Bansala419c792016-10-20 14:05:11 -070033
34#include "adreno.h"
35#include "adreno_iommu.h"
36#include "adreno_compat.h"
37#include "adreno_pm4types.h"
38#include "adreno_trace.h"
39
40#include "a3xx_reg.h"
Deepak Kumar84b9e032017-11-08 13:08:50 +053041#include "a6xx_reg.h"
Shrenuj Bansala419c792016-10-20 14:05:11 -070042#include "adreno_snapshot.h"
43
44/* Include the master list of GPU cores that are supported */
45#include "adreno-gpulist.h"
46#include "adreno_dispatch.h"
47
48#undef MODULE_PARAM_PREFIX
49#define MODULE_PARAM_PREFIX "adreno."
50
51static bool nopreempt;
52module_param(nopreempt, bool, 0444);
53MODULE_PARM_DESC(nopreempt, "Disable GPU preemption");
54
Shrenuj Bansalae672812016-02-24 14:17:30 -080055static bool swfdetect;
56module_param(swfdetect, bool, 0444);
57MODULE_PARM_DESC(swfdetect, "Enable soft fault detection");
58
Shrenuj Bansala419c792016-10-20 14:05:11 -070059#define DRIVER_VERSION_MAJOR 3
60#define DRIVER_VERSION_MINOR 1
61
Shrenuj Bansala419c792016-10-20 14:05:11 -070062#define KGSL_LOG_LEVEL_DEFAULT 3
63
64static void adreno_input_work(struct work_struct *work);
65static unsigned int counter_delta(struct kgsl_device *device,
66 unsigned int reg, unsigned int *counter);
67
68static struct devfreq_msm_adreno_tz_data adreno_tz_data = {
69 .bus = {
70 .max = 350,
71 },
72 .device_id = KGSL_DEVICE_3D0,
73};
74
75static const struct kgsl_functable adreno_functable;
76
77static struct adreno_device device_3d0 = {
78 .dev = {
79 KGSL_DEVICE_COMMON_INIT(device_3d0.dev),
80 .pwrscale = KGSL_PWRSCALE_INIT(&adreno_tz_data),
81 .name = DEVICE_3D0_NAME,
82 .id = KGSL_DEVICE_3D0,
Kyle Pieferb1027b02017-02-10 13:58:58 -080083 .gmu = {
84 .load_mode = TCM_BOOT,
85 },
Shrenuj Bansala419c792016-10-20 14:05:11 -070086 .pwrctrl = {
87 .irq_name = "kgsl_3d0_irq",
88 },
89 .iomemname = "kgsl_3d0_reg_memory",
90 .shadermemname = "kgsl_3d0_shader_memory",
91 .ftbl = &adreno_functable,
92 .cmd_log = KGSL_LOG_LEVEL_DEFAULT,
93 .ctxt_log = KGSL_LOG_LEVEL_DEFAULT,
94 .drv_log = KGSL_LOG_LEVEL_DEFAULT,
95 .mem_log = KGSL_LOG_LEVEL_DEFAULT,
96 .pwr_log = KGSL_LOG_LEVEL_DEFAULT,
97 },
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -070098 .fw[0] = {
99 .fwvirt = NULL
100 },
101 .fw[1] = {
102 .fwvirt = NULL
103 },
Shrenuj Bansala419c792016-10-20 14:05:11 -0700104 .gmem_size = SZ_256K,
Shrenuj Bansala419c792016-10-20 14:05:11 -0700105 .ft_policy = KGSL_FT_DEFAULT_POLICY,
106 .ft_pf_policy = KGSL_FT_PAGEFAULT_DEFAULT_POLICY,
Shrenuj Bansala419c792016-10-20 14:05:11 -0700107 .long_ib_detect = 1,
108 .input_work = __WORK_INITIALIZER(device_3d0.input_work,
109 adreno_input_work),
110 .pwrctrl_flag = BIT(ADRENO_SPTP_PC_CTRL) | BIT(ADRENO_PPD_CTRL) |
111 BIT(ADRENO_LM_CTRL) | BIT(ADRENO_HWCG_CTRL) |
112 BIT(ADRENO_THROTTLING_CTRL),
113 .profile.enabled = false,
114 .active_list = LIST_HEAD_INIT(device_3d0.active_list),
115 .active_list_lock = __SPIN_LOCK_UNLOCKED(device_3d0.active_list_lock),
Sushmita Susheelendrab1976682016-11-07 14:21:11 -0700116 .gpu_llc_slice_enable = true,
Sushmita Susheelendrad3756c02017-01-11 15:05:40 -0700117 .gpuhtw_llc_slice_enable = true,
Harshdeep Dhatta8ec51c2017-09-21 17:24:08 -0600118 .preempt = {
119 .preempt_level = 1,
120 .skipsaverestore = 1,
121 .usesgmem = 1,
122 },
Shrenuj Bansala419c792016-10-20 14:05:11 -0700123};
124
125/* Ptr to array for the current set of fault detect registers */
126unsigned int *adreno_ft_regs;
127/* Total number of fault detect registers */
128unsigned int adreno_ft_regs_num;
129/* Ptr to array for the current fault detect registers values */
130unsigned int *adreno_ft_regs_val;
131/* Array of default fault detect registers */
132static unsigned int adreno_ft_regs_default[] = {
133 ADRENO_REG_RBBM_STATUS,
134 ADRENO_REG_CP_RB_RPTR,
135 ADRENO_REG_CP_IB1_BASE,
136 ADRENO_REG_CP_IB1_BUFSZ,
137 ADRENO_REG_CP_IB2_BASE,
138 ADRENO_REG_CP_IB2_BUFSZ
139};
140
141/* Nice level for the higher priority GPU start thread */
142int adreno_wake_nice = -7;
143
144/* Number of milliseconds to stay active active after a wake on touch */
145unsigned int adreno_wake_timeout = 100;
146
147/**
148 * adreno_readreg64() - Read a 64bit register by getting its offset from the
149 * offset array defined in gpudev node
150 * @adreno_dev: Pointer to the the adreno device
151 * @lo: lower 32bit register enum that is to be read
152 * @hi: higher 32bit register enum that is to be read
153 * @val: 64 bit Register value read is placed here
154 */
155void adreno_readreg64(struct adreno_device *adreno_dev,
156 enum adreno_regs lo, enum adreno_regs hi, uint64_t *val)
157{
158 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
159 unsigned int val_lo = 0, val_hi = 0;
160 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
161
162 if (adreno_checkreg_off(adreno_dev, lo))
163 kgsl_regread(device, gpudev->reg_offsets->offsets[lo], &val_lo);
164 if (adreno_checkreg_off(adreno_dev, hi))
165 kgsl_regread(device, gpudev->reg_offsets->offsets[hi], &val_hi);
166
167 *val = (val_lo | ((uint64_t)val_hi << 32));
168}
169
170/**
171 * adreno_writereg64() - Write a 64bit register by getting its offset from the
172 * offset array defined in gpudev node
173 * @adreno_dev: Pointer to the the adreno device
174 * @lo: lower 32bit register enum that is to be written
175 * @hi: higher 32bit register enum that is to be written
176 * @val: 64 bit value to write
177 */
178void adreno_writereg64(struct adreno_device *adreno_dev,
179 enum adreno_regs lo, enum adreno_regs hi, uint64_t val)
180{
181 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
182
183 if (adreno_checkreg_off(adreno_dev, lo))
184 kgsl_regwrite(KGSL_DEVICE(adreno_dev),
185 gpudev->reg_offsets->offsets[lo], lower_32_bits(val));
186 if (adreno_checkreg_off(adreno_dev, hi))
187 kgsl_regwrite(KGSL_DEVICE(adreno_dev),
188 gpudev->reg_offsets->offsets[hi], upper_32_bits(val));
189}
190
191/**
192 * adreno_get_rptr() - Get the current ringbuffer read pointer
193 * @rb: Pointer the ringbuffer to query
194 *
195 * Get the latest rptr
196 */
197unsigned int adreno_get_rptr(struct adreno_ringbuffer *rb)
198{
199 struct adreno_device *adreno_dev = ADRENO_RB_DEVICE(rb);
200 unsigned int rptr = 0;
201
202 if (adreno_is_a3xx(adreno_dev))
203 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR,
204 &rptr);
205 else {
206 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
207
208 kgsl_sharedmem_readl(&device->scratch, &rptr,
209 SCRATCH_RPTR_OFFSET(rb->id));
210 }
211
212 return rptr;
213}
214
215/**
216 * adreno_of_read_property() - Adreno read property
217 * @node: Device node
218 *
219 * Read a u32 property.
220 */
221static inline int adreno_of_read_property(struct device_node *node,
222 const char *prop, unsigned int *ptr)
223{
224 int ret = of_property_read_u32(node, prop, ptr);
225
226 if (ret)
227 KGSL_CORE_ERR("Unable to read '%s'\n", prop);
228 return ret;
229}
230
231static void __iomem *efuse_base;
232static size_t efuse_len;
233
234int adreno_efuse_map(struct adreno_device *adreno_dev)
235{
236 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
237 struct resource *res;
238
239 if (efuse_base != NULL)
240 return 0;
241
242 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
243 "qfprom_memory");
244
245 if (res == NULL)
246 return -ENODEV;
247
248 efuse_base = ioremap(res->start, resource_size(res));
249 if (efuse_base == NULL)
250 return -ENODEV;
251
252 efuse_len = resource_size(res);
253 return 0;
254}
255
256void adreno_efuse_unmap(struct adreno_device *adreno_dev)
257{
258 if (efuse_base != NULL) {
259 iounmap(efuse_base);
260 efuse_base = NULL;
261 efuse_len = 0;
262 }
263}
264
265int adreno_efuse_read_u32(struct adreno_device *adreno_dev, unsigned int offset,
266 unsigned int *val)
267{
268 if (efuse_base == NULL)
269 return -ENODEV;
270
271 if (offset >= efuse_len)
272 return -ERANGE;
273
274 if (val != NULL) {
275 *val = readl_relaxed(efuse_base + offset);
276 /* Make sure memory is updated before returning */
277 rmb();
278 }
279
280 return 0;
281}
282
283static int _get_counter(struct adreno_device *adreno_dev,
284 int group, int countable, unsigned int *lo,
285 unsigned int *hi)
286{
287 int ret = 0;
288
289 if (*lo == 0) {
290
291 ret = adreno_perfcounter_get(adreno_dev, group, countable,
292 lo, hi, PERFCOUNTER_FLAG_KERNEL);
293
294 if (ret) {
295 KGSL_DRV_ERR(KGSL_DEVICE(adreno_dev),
296 "Unable to allocate fault detect performance counter %d/%d\n",
297 group, countable);
298 KGSL_DRV_ERR(KGSL_DEVICE(adreno_dev),
299 "GPU fault detect will be less reliable\n");
300 }
301 }
302
303 return ret;
304}
305
306static inline void _put_counter(struct adreno_device *adreno_dev,
307 int group, int countable, unsigned int *lo,
308 unsigned int *hi)
309{
310 if (*lo != 0)
311 adreno_perfcounter_put(adreno_dev, group, countable,
312 PERFCOUNTER_FLAG_KERNEL);
313
314 *lo = 0;
315 *hi = 0;
316}
317
318/**
319 * adreno_fault_detect_start() - Allocate performance counters
320 * used for fast fault detection
321 * @adreno_dev: Pointer to an adreno_device structure
322 *
323 * Allocate the series of performance counters that should be periodically
324 * checked to verify that the GPU is still moving
325 */
326void adreno_fault_detect_start(struct adreno_device *adreno_dev)
327{
328 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
329 unsigned int i, j = ARRAY_SIZE(adreno_ft_regs_default);
330
331 if (!test_bit(ADRENO_DEVICE_SOFT_FAULT_DETECT, &adreno_dev->priv))
332 return;
333
334 if (adreno_dev->fast_hang_detect == 1)
335 return;
336
337 for (i = 0; i < gpudev->ft_perf_counters_count; i++) {
338 _get_counter(adreno_dev, gpudev->ft_perf_counters[i].counter,
339 gpudev->ft_perf_counters[i].countable,
340 &adreno_ft_regs[j + (i * 2)],
341 &adreno_ft_regs[j + ((i * 2) + 1)]);
342 }
343
344 adreno_dev->fast_hang_detect = 1;
345}
346
347/**
348 * adreno_fault_detect_stop() - Release performance counters
349 * used for fast fault detection
350 * @adreno_dev: Pointer to an adreno_device structure
351 *
352 * Release the counters allocated in adreno_fault_detect_start
353 */
354void adreno_fault_detect_stop(struct adreno_device *adreno_dev)
355{
356 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
357 unsigned int i, j = ARRAY_SIZE(adreno_ft_regs_default);
358
359 if (!test_bit(ADRENO_DEVICE_SOFT_FAULT_DETECT, &adreno_dev->priv))
360 return;
361
362 if (!adreno_dev->fast_hang_detect)
363 return;
364
365 for (i = 0; i < gpudev->ft_perf_counters_count; i++) {
366 _put_counter(adreno_dev, gpudev->ft_perf_counters[i].counter,
367 gpudev->ft_perf_counters[i].countable,
368 &adreno_ft_regs[j + (i * 2)],
369 &adreno_ft_regs[j + ((i * 2) + 1)]);
370
371 }
372
373 adreno_dev->fast_hang_detect = 0;
374}
375
376/*
377 * A workqueue callback responsible for actually turning on the GPU after a
378 * touch event. kgsl_pwrctrl_change_state(ACTIVE) is used without any
379 * active_count protection to avoid the need to maintain state. Either
380 * somebody will start using the GPU or the idle timer will fire and put the
381 * GPU back into slumber.
382 */
383static void adreno_input_work(struct work_struct *work)
384{
385 struct adreno_device *adreno_dev = container_of(work,
386 struct adreno_device, input_work);
387 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
388
389 mutex_lock(&device->mutex);
390
391 device->flags |= KGSL_FLAG_WAKE_ON_TOUCH;
392
393 /*
394 * Don't schedule adreno_start in a high priority workqueue, we are
395 * already in a workqueue which should be sufficient
396 */
397 kgsl_pwrctrl_change_state(device, KGSL_STATE_ACTIVE);
398
399 /*
400 * When waking up from a touch event we want to stay active long enough
401 * for the user to send a draw command. The default idle timer timeout
402 * is shorter than we want so go ahead and push the idle timer out
403 * further for this special case
404 */
405 mod_timer(&device->idle_timer,
406 jiffies + msecs_to_jiffies(adreno_wake_timeout));
407 mutex_unlock(&device->mutex);
408}
409
410/*
411 * Process input events and schedule work if needed. At this point we are only
412 * interested in groking EV_ABS touchscreen events
413 */
414static void adreno_input_event(struct input_handle *handle, unsigned int type,
415 unsigned int code, int value)
416{
417 struct kgsl_device *device = handle->handler->private;
418 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
419
420 /* Only consider EV_ABS (touch) events */
421 if (type != EV_ABS)
422 return;
423
424 /*
425 * Don't do anything if anything hasn't been rendered since we've been
426 * here before
427 */
428
429 if (device->flags & KGSL_FLAG_WAKE_ON_TOUCH)
430 return;
431
432 /*
433 * If the device is in nap, kick the idle timer to make sure that we
434 * don't go into slumber before the first render. If the device is
435 * already in slumber schedule the wake.
436 */
437
438 if (device->state == KGSL_STATE_NAP) {
439 /*
440 * Set the wake on touch bit to keep from coming back here and
441 * keeping the device in nap without rendering
442 */
443
444 device->flags |= KGSL_FLAG_WAKE_ON_TOUCH;
445
446 mod_timer(&device->idle_timer,
447 jiffies + device->pwrctrl.interval_timeout);
448 } else if (device->state == KGSL_STATE_SLUMBER) {
449 schedule_work(&adreno_dev->input_work);
450 }
451}
452
453#ifdef CONFIG_INPUT
454static int adreno_input_connect(struct input_handler *handler,
455 struct input_dev *dev, const struct input_device_id *id)
456{
457 struct input_handle *handle;
458 int ret;
459
460 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
461 if (handle == NULL)
462 return -ENOMEM;
463
464 handle->dev = dev;
465 handle->handler = handler;
466 handle->name = handler->name;
467
468 ret = input_register_handle(handle);
469 if (ret) {
470 kfree(handle);
471 return ret;
472 }
473
474 ret = input_open_device(handle);
475 if (ret) {
476 input_unregister_handle(handle);
477 kfree(handle);
478 }
479
480 return ret;
481}
482
483static void adreno_input_disconnect(struct input_handle *handle)
484{
485 input_close_device(handle);
486 input_unregister_handle(handle);
487 kfree(handle);
488}
489#else
490static int adreno_input_connect(struct input_handler *handler,
491 struct input_dev *dev, const struct input_device_id *id)
492{
493 return 0;
494}
495static void adreno_input_disconnect(struct input_handle *handle) {}
496#endif
497
498/*
499 * We are only interested in EV_ABS events so only register handlers for those
500 * input devices that have EV_ABS events
501 */
502static const struct input_device_id adreno_input_ids[] = {
503 {
504 .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
505 .evbit = { BIT_MASK(EV_ABS) },
506 /* assumption: MT_.._X & MT_.._Y are in the same long */
507 .absbit = { [BIT_WORD(ABS_MT_POSITION_X)] =
508 BIT_MASK(ABS_MT_POSITION_X) |
509 BIT_MASK(ABS_MT_POSITION_Y) },
510 },
511 { },
512};
513
514static struct input_handler adreno_input_handler = {
515 .event = adreno_input_event,
516 .connect = adreno_input_connect,
517 .disconnect = adreno_input_disconnect,
518 .name = "kgsl",
519 .id_table = adreno_input_ids,
520};
521
Shrenuj Bansala419c792016-10-20 14:05:11 -0700522/*
523 * _soft_reset() - Soft reset GPU
524 * @adreno_dev: Pointer to adreno device
525 *
526 * Soft reset the GPU by doing a AHB write of value 1 to RBBM_SW_RESET
527 * register. This is used when we want to reset the GPU without
528 * turning off GFX power rail. The reset when asserted resets
529 * all the HW logic, restores GPU registers to default state and
530 * flushes out pending VBIF transactions.
531 */
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -0700532static int _soft_reset(struct adreno_device *adreno_dev)
Shrenuj Bansala419c792016-10-20 14:05:11 -0700533{
534 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
535 unsigned int reg;
536
537 /*
538 * On a530 v1 RBBM cannot be reset in soft reset.
539 * Reset all blocks except RBBM for a530v1.
540 */
541 if (adreno_is_a530v1(adreno_dev)) {
542 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_BLOCK_SW_RESET_CMD,
543 0xFFDFFC0);
544 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_BLOCK_SW_RESET_CMD2,
545 0x1FFFFFFF);
546 } else {
547
548 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_SW_RESET_CMD, 1);
549 /*
550 * Do a dummy read to get a brief read cycle delay for the
551 * reset to take effect
552 */
553 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_SW_RESET_CMD, &reg);
554 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_SW_RESET_CMD, 0);
555 }
556
557 /* The SP/TP regulator gets turned off after a soft reset */
558
559 if (gpudev->regulator_enable)
560 gpudev->regulator_enable(adreno_dev);
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -0700561
562 return 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700563}
564
Kyle Piefer8fe58df2017-09-12 09:19:28 -0700565/**
566 * adreno_irqctrl() - Enables/disables the RBBM interrupt mask
567 * @adreno_dev: Pointer to an adreno_device
568 * @state: 1 for masked or 0 for unmasked
569 * Power: The caller of this function must make sure to use OOBs
570 * so that we know that the GPU is powered on
571 */
Shrenuj Bansala419c792016-10-20 14:05:11 -0700572void adreno_irqctrl(struct adreno_device *adreno_dev, int state)
573{
574 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
575 unsigned int mask = state ? gpudev->irq->mask : 0;
576
577 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_INT_0_MASK, mask);
578}
579
580/*
581 * adreno_hang_int_callback() - Isr for fatal interrupts that hang GPU
582 * @adreno_dev: Pointer to device
583 * @bit: Interrupt bit
584 */
585void adreno_hang_int_callback(struct adreno_device *adreno_dev, int bit)
586{
587 KGSL_DRV_CRIT_RATELIMIT(KGSL_DEVICE(adreno_dev),
588 "MISC: GPU hang detected\n");
589 adreno_irqctrl(adreno_dev, 0);
590
591 /* Trigger a fault in the dispatcher - this will effect a restart */
592 adreno_set_gpu_fault(adreno_dev, ADRENO_HARD_FAULT);
593 adreno_dispatcher_schedule(KGSL_DEVICE(adreno_dev));
594}
595
596/*
597 * adreno_cp_callback() - CP interrupt handler
598 * @adreno_dev: Adreno device pointer
599 * @irq: irq number
600 *
601 * Handle the cp interrupt generated by GPU.
602 */
603void adreno_cp_callback(struct adreno_device *adreno_dev, int bit)
604{
605 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
606
607 adreno_dispatcher_schedule(device);
608}
609
610static irqreturn_t adreno_irq_handler(struct kgsl_device *device)
611{
612 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
613 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
614 struct adreno_irq *irq_params = gpudev->irq;
615 irqreturn_t ret = IRQ_NONE;
Kyle Piefer5e1b78bd2017-10-19 13:22:10 -0700616 unsigned int status = 0, fence = 0, fence_retries = 0, tmp, int_bit;
Harshdeep Dhatte8046962017-11-10 15:45:24 -0700617 unsigned int status_retries = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700618 int i;
619
Deepak Kumar273c5712017-01-03 21:49:03 +0530620 atomic_inc(&adreno_dev->pending_irq_refcnt);
621 /* Ensure this increment is done before the IRQ status is updated */
622 smp_mb__after_atomic();
623
Carter Cooperdf7ba702017-03-20 11:28:04 -0600624 /*
625 * On A6xx, the GPU can power down once the INT_0_STATUS is read
626 * below. But there still might be some register reads required
627 * so force the GMU/GPU into KEEPALIVE mode until done with the ISR.
628 */
629 if (gpudev->gpu_keepalive)
630 gpudev->gpu_keepalive(adreno_dev, true);
631
Kyle Pieferda0fa542017-08-04 13:39:40 -0700632 /*
633 * If the AHB fence is not in ALLOW mode when we receive an RBBM
Kyle Piefer5e1b78bd2017-10-19 13:22:10 -0700634 * interrupt, something went wrong. This means that we cannot proceed
635 * since the IRQ status and clear registers are not accessible.
636 * This is usually harmless because the GMU will abort power collapse
637 * and change the fence back to ALLOW. Poll so that this can happen.
Kyle Pieferda0fa542017-08-04 13:39:40 -0700638 */
Kyle Piefer5e1b78bd2017-10-19 13:22:10 -0700639 if (kgsl_gmu_isenabled(device)) {
640 do {
641 adreno_readreg(adreno_dev,
642 ADRENO_REG_GMU_AO_AHB_FENCE_CTRL,
643 &fence);
644
645 if (fence_retries == FENCE_RETRY_MAX) {
646 KGSL_DRV_CRIT_RATELIMIT(device,
647 "AHB fence stuck in ISR\n");
648 return ret;
649 }
650 fence_retries++;
651 } while (fence != 0);
Kyle Pieferda0fa542017-08-04 13:39:40 -0700652 }
653
Shrenuj Bansala419c792016-10-20 14:05:11 -0700654 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_INT_0_STATUS, &status);
655
656 /*
Harshdeep Dhatte8046962017-11-10 15:45:24 -0700657 * Read status again to make sure the bits aren't transitory.
658 * Transitory bits mean that they are spurious interrupts and are
659 * seen while preemption is on going. Empirical experiments have
660 * shown that the transitory bits are a timing thing and they
661 * go away in the small time window between two or three consecutive
662 * reads. If they don't go away, log the message and return.
663 */
664 while (status_retries < STATUS_RETRY_MAX) {
665 unsigned int new_status;
666
667 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_INT_0_STATUS,
668 &new_status);
669
670 if (status == new_status)
671 break;
672
673 status = new_status;
674 status_retries++;
675 }
676
677 if (status_retries == STATUS_RETRY_MAX) {
678 KGSL_DRV_CRIT_RATELIMIT(device, "STATUS bits are not stable\n");
679 return ret;
680 }
681
682 /*
Shrenuj Bansala419c792016-10-20 14:05:11 -0700683 * Clear all the interrupt bits but ADRENO_INT_RBBM_AHB_ERROR. Because
684 * even if we clear it here, it will stay high until it is cleared
685 * in its respective handler. Otherwise, the interrupt handler will
686 * fire again.
687 */
688 int_bit = ADRENO_INT_BIT(adreno_dev, ADRENO_INT_RBBM_AHB_ERROR);
689 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_INT_CLEAR_CMD,
690 status & ~int_bit);
691
692 /* Loop through all set interrupts and call respective handlers */
693 for (tmp = status; tmp != 0;) {
694 i = fls(tmp) - 1;
695
696 if (irq_params->funcs[i].func != NULL) {
697 if (irq_params->mask & BIT(i))
698 irq_params->funcs[i].func(adreno_dev, i);
699 } else
700 KGSL_DRV_CRIT_RATELIMIT(device,
701 "Unhandled interrupt bit %x\n", i);
702
703 ret = IRQ_HANDLED;
704
705 tmp &= ~BIT(i);
706 }
707
708 gpudev->irq_trace(adreno_dev, status);
709
710 /*
711 * Clear ADRENO_INT_RBBM_AHB_ERROR bit after this interrupt has been
712 * cleared in its respective handler
713 */
714 if (status & int_bit)
715 adreno_writereg(adreno_dev, ADRENO_REG_RBBM_INT_CLEAR_CMD,
716 int_bit);
717
Carter Cooperdf7ba702017-03-20 11:28:04 -0600718 /* Turn off the KEEPALIVE vote from earlier unless hard fault set */
719 if (gpudev->gpu_keepalive) {
720 /* If hard fault, then let snapshot turn off the keepalive */
721 if (!(adreno_gpu_fault(adreno_dev) & ADRENO_HARD_FAULT))
722 gpudev->gpu_keepalive(adreno_dev, false);
723 }
724
Deepak Kumar273c5712017-01-03 21:49:03 +0530725 /* Make sure the regwrites are done before the decrement */
726 smp_mb__before_atomic();
727 atomic_dec(&adreno_dev->pending_irq_refcnt);
728 /* Ensure other CPUs see the decrement */
729 smp_mb__after_atomic();
730
Shrenuj Bansala419c792016-10-20 14:05:11 -0700731 return ret;
732
733}
734
735static inline bool _rev_match(unsigned int id, unsigned int entry)
736{
737 return (entry == ANY_ID || entry == id);
738}
739
740static inline const struct adreno_gpu_core *_get_gpu_core(unsigned int chipid)
741{
742 unsigned int core = ADRENO_CHIPID_CORE(chipid);
743 unsigned int major = ADRENO_CHIPID_MAJOR(chipid);
744 unsigned int minor = ADRENO_CHIPID_MINOR(chipid);
745 unsigned int patchid = ADRENO_CHIPID_PATCH(chipid);
746 int i;
747
748 for (i = 0; i < ARRAY_SIZE(adreno_gpulist); i++) {
749 if (core == adreno_gpulist[i].core &&
750 _rev_match(major, adreno_gpulist[i].major) &&
751 _rev_match(minor, adreno_gpulist[i].minor) &&
752 _rev_match(patchid, adreno_gpulist[i].patchid))
753 return &adreno_gpulist[i];
754 }
755
756 return NULL;
757}
758
Deepak Kumar65613682018-01-03 14:55:00 +0530759static struct {
760 unsigned int quirk;
761 const char *prop;
762} adreno_quirks[] = {
763 { ADRENO_QUIRK_TWO_PASS_USE_WFI, "qcom,gpu-quirk-two-pass-use-wfi" },
764 { ADRENO_QUIRK_IOMMU_SYNC, "qcom,gpu-quirk-iommu-sync" },
765 { ADRENO_QUIRK_CRITICAL_PACKETS, "qcom,gpu-quirk-critical-packets" },
766 { ADRENO_QUIRK_FAULT_DETECT_MASK, "qcom,gpu-quirk-fault-detect-mask" },
767 { ADRENO_QUIRK_DISABLE_RB_DP2CLOCKGATING,
768 "qcom,gpu-quirk-dp2clockgating-disable" },
769 { ADRENO_QUIRK_DISABLE_LMLOADKILL,
770 "qcom,gpu-quirk-lmloadkill-disable" },
771 { ADRENO_QUIRK_HFI_USE_REG, "qcom,gpu-quirk-hfi-use-reg" },
772 { ADRENO_QUIRK_SECVID_SET_ONCE, "qcom,gpu-quirk-secvid-set-once" },
773 { ADRENO_QUIRK_LIMIT_UCHE_GBIF_RW,
774 "qcom,gpu-quirk-limit-uche-gbif-rw" },
775};
776
777#if defined(CONFIG_NVMEM) && defined(CONFIG_QCOM_QFPROM)
778static struct device_node *
779adreno_get_soc_hw_revision_node(struct platform_device *pdev)
780{
781 struct device_node *node, *child;
782 struct nvmem_cell *cell;
783 ssize_t len;
784 u32 *buf, hw_rev, rev;
785
786 node = of_find_node_by_name(pdev->dev.of_node, "qcom,soc-hw-revisions");
787 if (node == NULL)
788 goto err;
789
790 /* read the soc hw revision and select revision node */
791 cell = nvmem_cell_get(&pdev->dev, "minor_rev");
792 if (IS_ERR_OR_NULL(cell)) {
793 if (PTR_ERR(cell) == -EPROBE_DEFER)
794 return (void *)cell;
795
796 KGSL_CORE_ERR("Unable to get nvmem cell: ret=%ld\n",
797 PTR_ERR(cell));
798 goto err;
799 }
800
801 buf = nvmem_cell_read(cell, &len);
802 nvmem_cell_put(cell);
803
804 if (IS_ERR_OR_NULL(buf)) {
805 KGSL_CORE_ERR("Unable to read nvmem cell: ret=%ld\n",
806 PTR_ERR(buf));
807 goto err;
808 }
809
810 hw_rev = *buf;
811 kfree(buf);
812
813 for_each_child_of_node(node, child) {
814 if (of_property_read_u32(child, "reg", &rev))
815 continue;
816
817 if (rev == hw_rev)
818 return child;
819 }
820
821err:
822 /* fall back to parent node */
823 return pdev->dev.of_node;
824}
825#else
826static struct device_node *
827adreno_get_soc_hw_revision_node(struct platform_device *pdev)
828{
829 return pdev->dev.of_node;
830}
831#endif
832
833
834static int adreno_update_soc_hw_revision_quirks(
835 struct adreno_device *adreno_dev, struct platform_device *pdev)
836{
837 struct device_node *node;
838 int i;
839
840 node = adreno_get_soc_hw_revision_node(pdev);
841 if (IS_ERR(node))
842 return PTR_ERR(node);
843
844 /* get chip id, fall back to parent if revision node does not have it */
845 if (of_property_read_u32(node, "qcom,chipid", &adreno_dev->chipid))
846 if (of_property_read_u32(pdev->dev.of_node,
847 "qcom,chipid", &adreno_dev->chipid))
848 KGSL_DRV_FATAL(KGSL_DEVICE(adreno_dev),
849 "No GPU chip ID was specified\n");
850
851 /* update quirk */
852 for (i = 0; i < ARRAY_SIZE(adreno_quirks); i++) {
853 if (of_property_read_bool(node, adreno_quirks[i].prop))
854 adreno_dev->quirks |= adreno_quirks[i].quirk;
855 }
856
857 return 0;
858}
859
Shrenuj Bansala419c792016-10-20 14:05:11 -0700860static void
861adreno_identify_gpu(struct adreno_device *adreno_dev)
862{
863 const struct adreno_reg_offsets *reg_offsets;
864 struct adreno_gpudev *gpudev;
865 int i;
866
Shrenuj Bansala419c792016-10-20 14:05:11 -0700867 adreno_dev->gpucore = _get_gpu_core(adreno_dev->chipid);
868
869 if (adreno_dev->gpucore == NULL)
870 KGSL_DRV_FATAL(KGSL_DEVICE(adreno_dev),
871 "Unknown GPU chip ID %8.8X\n", adreno_dev->chipid);
872
873 /*
874 * The gmem size might be dynamic when ocmem is involved so copy it out
875 * of the gpu device
876 */
877
878 adreno_dev->gmem_size = adreno_dev->gpucore->gmem_size;
879
880 /*
881 * Initialize uninitialzed gpu registers, only needs to be done once
882 * Make all offsets that are not initialized to ADRENO_REG_UNUSED
883 */
884
885 gpudev = ADRENO_GPU_DEVICE(adreno_dev);
886 reg_offsets = gpudev->reg_offsets;
887
888 for (i = 0; i < ADRENO_REG_REGISTER_MAX; i++) {
889 if (reg_offsets->offset_0 != i && !reg_offsets->offsets[i])
890 reg_offsets->offsets[i] = ADRENO_REG_UNUSED;
891 }
892
893 /* Do target specific identification */
894 if (gpudev->platform_setup != NULL)
895 gpudev->platform_setup(adreno_dev);
896}
897
898static const struct platform_device_id adreno_id_table[] = {
899 { DEVICE_3D0_NAME, (unsigned long) &device_3d0, },
900 {},
901};
902
903MODULE_DEVICE_TABLE(platform, adreno_id_table);
904
905static const struct of_device_id adreno_match_table[] = {
906 { .compatible = "qcom,kgsl-3d0", .data = &device_3d0 },
907 {}
908};
909
910static int adreno_of_parse_pwrlevels(struct adreno_device *adreno_dev,
911 struct device_node *node)
912{
913 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
914 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
915 struct device_node *child;
916
917 pwr->num_pwrlevels = 0;
918
919 for_each_child_of_node(node, child) {
920 unsigned int index;
921 struct kgsl_pwrlevel *level;
922
923 if (adreno_of_read_property(child, "reg", &index))
924 return -EINVAL;
925
926 if (index >= KGSL_MAX_PWRLEVELS) {
927 KGSL_CORE_ERR("Pwrlevel index %d is out of range\n",
928 index);
929 continue;
930 }
931
932 if (index >= pwr->num_pwrlevels)
933 pwr->num_pwrlevels = index + 1;
934
935 level = &pwr->pwrlevels[index];
936
937 if (adreno_of_read_property(child, "qcom,gpu-freq",
938 &level->gpu_freq))
939 return -EINVAL;
940
941 if (adreno_of_read_property(child, "qcom,bus-freq",
942 &level->bus_freq))
943 return -EINVAL;
944
945 if (of_property_read_u32(child, "qcom,bus-min",
946 &level->bus_min))
947 level->bus_min = level->bus_freq;
948
949 if (of_property_read_u32(child, "qcom,bus-max",
950 &level->bus_max))
951 level->bus_max = level->bus_freq;
952 }
953
954 return 0;
955}
956
957
958static void adreno_of_get_initial_pwrlevel(struct adreno_device *adreno_dev,
959 struct device_node *node)
960{
961 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
962 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
963 int init_level = 1;
964
965 of_property_read_u32(node, "qcom,initial-pwrlevel", &init_level);
966
967 if (init_level < 0 || init_level > pwr->num_pwrlevels)
968 init_level = 1;
969
970 pwr->active_pwrlevel = init_level;
971 pwr->default_pwrlevel = init_level;
972}
973
974static int adreno_of_get_legacy_pwrlevels(struct adreno_device *adreno_dev,
975 struct device_node *parent)
976{
977 struct device_node *node;
978 int ret;
979
980 node = of_find_node_by_name(parent, "qcom,gpu-pwrlevels");
981
982 if (node == NULL) {
983 KGSL_CORE_ERR("Unable to find 'qcom,gpu-pwrlevels'\n");
984 return -EINVAL;
985 }
986
987 ret = adreno_of_parse_pwrlevels(adreno_dev, node);
988 if (ret == 0)
989 adreno_of_get_initial_pwrlevel(adreno_dev, parent);
990 return ret;
991}
992
993static int adreno_of_get_pwrlevels(struct adreno_device *adreno_dev,
994 struct device_node *parent)
995{
996 struct device_node *node, *child;
Hareesh Gundu7d536522017-08-24 20:20:56 +0530997 unsigned int bin = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -0700998
999 node = of_find_node_by_name(parent, "qcom,gpu-pwrlevel-bins");
1000 if (node == NULL)
1001 return adreno_of_get_legacy_pwrlevels(adreno_dev, parent);
1002
1003 for_each_child_of_node(node, child) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07001004
1005 if (of_property_read_u32(child, "qcom,speed-bin", &bin))
1006 continue;
1007
1008 if (bin == adreno_dev->speed_bin) {
1009 int ret;
1010
1011 ret = adreno_of_parse_pwrlevels(adreno_dev, child);
1012 if (ret == 0)
1013 adreno_of_get_initial_pwrlevel(adreno_dev,
1014 child);
1015 return ret;
1016 }
1017 }
1018
Hareesh Gundu7d536522017-08-24 20:20:56 +05301019 KGSL_CORE_ERR("GPU speed_bin:%d mismatch for efused bin:%d\n",
1020 adreno_dev->speed_bin, bin);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001021 return -ENODEV;
1022}
1023
1024static inline struct adreno_device *adreno_get_dev(struct platform_device *pdev)
1025{
1026 const struct of_device_id *of_id =
1027 of_match_device(adreno_match_table, &pdev->dev);
1028
1029 return of_id ? (struct adreno_device *) of_id->data : NULL;
1030}
1031
Shrenuj Bansala419c792016-10-20 14:05:11 -07001032static int adreno_of_get_power(struct adreno_device *adreno_dev,
1033 struct platform_device *pdev)
1034{
1035 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1036 struct device_node *node = pdev->dev.of_node;
Kyle Pieferb1027b02017-02-10 13:58:58 -08001037 struct resource *res;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001038 unsigned int timeout;
1039
1040 if (of_property_read_string(node, "label", &pdev->name)) {
1041 KGSL_CORE_ERR("Unable to read 'label'\n");
1042 return -EINVAL;
1043 }
1044
1045 if (adreno_of_read_property(node, "qcom,id", &pdev->id))
1046 return -EINVAL;
1047
Kyle Pieferb1027b02017-02-10 13:58:58 -08001048 /* Get starting physical address of device registers */
1049 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
1050 device->iomemname);
1051 if (res == NULL) {
1052 KGSL_DRV_ERR(device, "platform_get_resource_byname failed\n");
1053 return -EINVAL;
1054 }
1055 if (res->start == 0 || resource_size(res) == 0) {
1056 KGSL_DRV_ERR(device, "dev %d invalid register region\n",
1057 device->id);
1058 return -EINVAL;
1059 }
1060
1061 device->reg_phys = res->start;
1062 device->reg_len = resource_size(res);
1063
Shrenuj Bansala419c792016-10-20 14:05:11 -07001064 if (adreno_of_get_pwrlevels(adreno_dev, node))
1065 return -EINVAL;
1066
1067 /* get pm-qos-active-latency, set it to default if not found */
1068 if (of_property_read_u32(node, "qcom,pm-qos-active-latency",
1069 &device->pwrctrl.pm_qos_active_latency))
1070 device->pwrctrl.pm_qos_active_latency = 501;
1071
1072 /* get pm-qos-cpu-mask-latency, set it to default if not found */
1073 if (of_property_read_u32(node, "qcom,l2pc-cpu-mask-latency",
1074 &device->pwrctrl.pm_qos_cpu_mask_latency))
1075 device->pwrctrl.pm_qos_cpu_mask_latency = 501;
1076
1077 /* get pm-qos-wakeup-latency, set it to default if not found */
1078 if (of_property_read_u32(node, "qcom,pm-qos-wakeup-latency",
1079 &device->pwrctrl.pm_qos_wakeup_latency))
1080 device->pwrctrl.pm_qos_wakeup_latency = 101;
1081
1082 if (of_property_read_u32(node, "qcom,idle-timeout", &timeout))
1083 timeout = 80;
1084
1085 device->pwrctrl.interval_timeout = msecs_to_jiffies(timeout);
1086
1087 device->pwrctrl.bus_control = of_property_read_bool(node,
1088 "qcom,bus-control");
1089
Hareesh Gundu5648ead2017-07-28 16:48:00 +05301090 device->pwrctrl.input_disable = of_property_read_bool(node,
1091 "qcom,disable-wake-on-touch");
1092
Shrenuj Bansala419c792016-10-20 14:05:11 -07001093 return 0;
1094}
1095
1096#ifdef CONFIG_QCOM_OCMEM
1097static int
1098adreno_ocmem_malloc(struct adreno_device *adreno_dev)
1099{
1100 if (!ADRENO_FEATURE(adreno_dev, ADRENO_USES_OCMEM))
1101 return 0;
1102
1103 if (adreno_dev->ocmem_hdl == NULL) {
1104 adreno_dev->ocmem_hdl =
1105 ocmem_allocate(OCMEM_GRAPHICS, adreno_dev->gmem_size);
1106 if (IS_ERR_OR_NULL(adreno_dev->ocmem_hdl)) {
1107 adreno_dev->ocmem_hdl = NULL;
1108 return -ENOMEM;
1109 }
1110
1111 adreno_dev->gmem_size = adreno_dev->ocmem_hdl->len;
1112 adreno_dev->gmem_base = adreno_dev->ocmem_hdl->addr;
1113 }
1114
1115 return 0;
1116}
1117
1118static void
1119adreno_ocmem_free(struct adreno_device *adreno_dev)
1120{
1121 if (adreno_dev->ocmem_hdl != NULL) {
1122 ocmem_free(OCMEM_GRAPHICS, adreno_dev->ocmem_hdl);
1123 adreno_dev->ocmem_hdl = NULL;
1124 }
1125}
1126#else
1127static int
1128adreno_ocmem_malloc(struct adreno_device *adreno_dev)
1129{
1130 return 0;
1131}
1132
1133static void
1134adreno_ocmem_free(struct adreno_device *adreno_dev)
1135{
1136}
1137#endif
1138
Lynus Vaz9ed8cf92017-09-21 21:55:34 +05301139static void adreno_cx_dbgc_probe(struct kgsl_device *device)
1140{
1141 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1142 struct resource *res;
1143
1144 res = platform_get_resource_byname(device->pdev, IORESOURCE_MEM,
1145 "kgsl_3d0_cx_dbgc_memory");
1146
1147 if (res == NULL)
1148 return;
1149
1150 adreno_dev->cx_dbgc_base = res->start - device->reg_phys;
1151 adreno_dev->cx_dbgc_len = resource_size(res);
1152 adreno_dev->cx_dbgc_virt = devm_ioremap(device->dev,
1153 device->reg_phys +
1154 adreno_dev->cx_dbgc_base,
1155 adreno_dev->cx_dbgc_len);
1156
1157 if (adreno_dev->cx_dbgc_virt == NULL)
1158 KGSL_DRV_WARN(device, "cx_dbgc ioremap failed\n");
1159}
1160
Sunil Khatrifa43f4a2017-12-12 17:20:53 +05301161static bool adreno_is_gpu_disabled(struct adreno_device *adreno_dev)
1162{
1163 unsigned int row0;
1164 unsigned int pte_row0_msb[3];
1165 int ret;
1166 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1167
1168 if (of_property_read_u32_array(device->pdev->dev.of_node,
1169 "qcom,gpu-disable-fuse", pte_row0_msb, 3))
1170 return false;
1171 /*
1172 * Read the fuse value to disable GPU driver if fuse
1173 * is blown. By default(fuse value is 0) GPU is enabled.
1174 */
1175 if (adreno_efuse_map(adreno_dev))
1176 return false;
1177
1178 ret = adreno_efuse_read_u32(adreno_dev, pte_row0_msb[0], &row0);
1179 adreno_efuse_unmap(adreno_dev);
1180
1181 if (ret)
1182 return false;
1183
1184 return (row0 >> pte_row0_msb[2]) &
1185 pte_row0_msb[1] ? true : false;
1186}
1187
Shrenuj Bansala419c792016-10-20 14:05:11 -07001188static int adreno_probe(struct platform_device *pdev)
1189{
1190 struct kgsl_device *device;
1191 struct adreno_device *adreno_dev;
1192 int status;
1193
1194 adreno_dev = adreno_get_dev(pdev);
1195
1196 if (adreno_dev == NULL) {
1197 pr_err("adreno: qcom,kgsl-3d0 does not exist in the device tree");
1198 return -ENODEV;
1199 }
1200
1201 device = KGSL_DEVICE(adreno_dev);
1202 device->pdev = pdev;
1203
Sunil Khatrifa43f4a2017-12-12 17:20:53 +05301204 if (adreno_is_gpu_disabled(adreno_dev)) {
1205 pr_err("adreno: GPU is disabled on this device");
1206 return -ENODEV;
1207 }
1208
Deepak Kumar65613682018-01-03 14:55:00 +05301209 status = adreno_update_soc_hw_revision_quirks(adreno_dev, pdev);
1210 if (status) {
1211 device->pdev = NULL;
1212 return status;
1213 }
1214
Shrenuj Bansala419c792016-10-20 14:05:11 -07001215 /* Get the chip ID from the DT and set up target specific parameters */
1216 adreno_identify_gpu(adreno_dev);
1217
1218 status = adreno_of_get_power(adreno_dev, pdev);
1219 if (status) {
1220 device->pdev = NULL;
1221 return status;
1222 }
1223
1224 /*
Kyle Pieferb1027b02017-02-10 13:58:58 -08001225 * Probe/init GMU after initial gpu power probe
1226 * Another part of GPU power probe in platform_probe
1227 * needs GMU initialized.
1228 */
1229 status = gmu_probe(device);
1230 if (status != 0 && status != -ENXIO) {
1231 device->pdev = NULL;
1232 return status;
1233 }
1234
1235 /*
Shrenuj Bansala419c792016-10-20 14:05:11 -07001236 * The SMMU APIs use unsigned long for virtual addresses which means
1237 * that we cannot use 64 bit virtual addresses on a 32 bit kernel even
1238 * though the hardware and the rest of the KGSL driver supports it.
1239 */
1240 if (adreno_support_64bit(adreno_dev))
1241 device->mmu.features |= KGSL_MMU_64BIT;
1242
1243 status = kgsl_device_platform_probe(device);
1244 if (status) {
1245 device->pdev = NULL;
1246 return status;
1247 }
1248
Lynus Vaz9ed8cf92017-09-21 21:55:34 +05301249 /* Probe for the optional CX_DBGC block */
1250 adreno_cx_dbgc_probe(device);
1251
Shrenuj Bansala419c792016-10-20 14:05:11 -07001252 /*
1253 * qcom,iommu-secure-id is used to identify MMUs that can handle secure
1254 * content but that is only part of the story - the GPU also has to be
1255 * able to handle secure content. Unfortunately in a classic catch-22
1256 * we cannot identify the GPU until after the DT is parsed. tl;dr -
1257 * check the GPU capabilities here and modify mmu->secured accordingly
1258 */
1259
1260 if (!ADRENO_FEATURE(adreno_dev, ADRENO_CONTENT_PROTECTION))
1261 device->mmu.secured = false;
1262
Shrenuj Bansal4fd6a562017-08-07 15:12:54 -07001263 if (ADRENO_FEATURE(adreno_dev, ADRENO_IOCOHERENT))
1264 device->mmu.features |= KGSL_MMU_IO_COHERENT;
1265
Shrenuj Bansala419c792016-10-20 14:05:11 -07001266 status = adreno_ringbuffer_probe(adreno_dev, nopreempt);
1267 if (status)
1268 goto out;
1269
1270 status = adreno_dispatcher_init(adreno_dev);
1271 if (status)
1272 goto out;
1273
1274 adreno_debugfs_init(adreno_dev);
1275 adreno_profile_init(adreno_dev);
1276
1277 adreno_sysfs_init(adreno_dev);
1278
1279 kgsl_pwrscale_init(&pdev->dev, CONFIG_QCOM_ADRENO_DEFAULT_GOVERNOR);
1280
1281 /* Initialize coresight for the target */
1282 adreno_coresight_init(adreno_dev);
1283
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001284 /* Get the system cache slice descriptor for GPU */
1285 adreno_dev->gpu_llc_slice = adreno_llc_getd(&pdev->dev, "gpu");
1286 if (IS_ERR(adreno_dev->gpu_llc_slice)) {
1287 KGSL_DRV_WARN(device,
1288 "Failed to get GPU LLC slice descriptor (%ld)\n",
1289 PTR_ERR(adreno_dev->gpu_llc_slice));
1290 adreno_dev->gpu_llc_slice = NULL;
1291 }
1292
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001293 /* Get the system cache slice descriptor for GPU pagetables */
1294 adreno_dev->gpuhtw_llc_slice = adreno_llc_getd(&pdev->dev, "gpuhtw");
1295 if (IS_ERR(adreno_dev->gpuhtw_llc_slice)) {
1296 KGSL_DRV_WARN(device,
1297 "Failed to get gpuhtw LLC slice descriptor (%ld)\n",
1298 PTR_ERR(adreno_dev->gpuhtw_llc_slice));
1299 adreno_dev->gpuhtw_llc_slice = NULL;
1300 }
1301
Shrenuj Bansala419c792016-10-20 14:05:11 -07001302#ifdef CONFIG_INPUT
Hareesh Gundu5648ead2017-07-28 16:48:00 +05301303 if (!device->pwrctrl.input_disable) {
1304 adreno_input_handler.private = device;
1305 /*
1306 * It isn't fatal if we cannot register the input handler. Sad,
1307 * perhaps, but not fatal
1308 */
1309 if (input_register_handler(&adreno_input_handler)) {
1310 adreno_input_handler.private = NULL;
1311 KGSL_DRV_ERR(device,
1312 "Unable to register the input handler\n");
1313 }
1314 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07001315#endif
1316out:
1317 if (status) {
1318 adreno_ringbuffer_close(adreno_dev);
1319 kgsl_device_platform_remove(device);
1320 device->pdev = NULL;
1321 }
1322
1323 return status;
1324}
1325
1326static void _adreno_free_memories(struct adreno_device *adreno_dev)
1327{
1328 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -07001329 struct adreno_firmware *pfp_fw = ADRENO_FW(adreno_dev, ADRENO_FW_PFP);
1330 struct adreno_firmware *pm4_fw = ADRENO_FW(adreno_dev, ADRENO_FW_PM4);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001331
1332 if (test_bit(ADRENO_DEVICE_DRAWOBJ_PROFILE, &adreno_dev->priv))
1333 kgsl_free_global(device, &adreno_dev->profile_buffer);
1334
1335 /* Free local copies of firmware and other command streams */
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -07001336 kfree(pfp_fw->fwvirt);
1337 pfp_fw->fwvirt = NULL;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001338
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -07001339 kfree(pm4_fw->fwvirt);
1340 pm4_fw->fwvirt = NULL;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001341
1342 kfree(adreno_dev->gpmu_cmds);
1343 adreno_dev->gpmu_cmds = NULL;
1344
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -07001345 kgsl_free_global(device, &pfp_fw->memdesc);
1346 kgsl_free_global(device, &pm4_fw->memdesc);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001347}
1348
1349static int adreno_remove(struct platform_device *pdev)
1350{
1351 struct adreno_device *adreno_dev = adreno_get_dev(pdev);
1352 struct adreno_gpudev *gpudev;
1353 struct kgsl_device *device;
1354
1355 if (adreno_dev == NULL)
1356 return 0;
1357
1358 device = KGSL_DEVICE(adreno_dev);
1359 gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1360
1361 if (gpudev->remove != NULL)
1362 gpudev->remove(adreno_dev);
1363
1364 /* The memory is fading */
1365 _adreno_free_memories(adreno_dev);
1366
1367#ifdef CONFIG_INPUT
Hareesh Gundu5648ead2017-07-28 16:48:00 +05301368 if (adreno_input_handler.private)
1369 input_unregister_handler(&adreno_input_handler);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001370#endif
1371 adreno_sysfs_close(adreno_dev);
1372
1373 adreno_coresight_remove(adreno_dev);
1374 adreno_profile_close(adreno_dev);
1375
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001376 /* Release the system cache slice descriptor */
1377 if (adreno_dev->gpu_llc_slice)
1378 adreno_llc_putd(adreno_dev->gpu_llc_slice);
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07001379 if (adreno_dev->gpuhtw_llc_slice)
1380 adreno_llc_putd(adreno_dev->gpuhtw_llc_slice);
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001381
Shrenuj Bansala419c792016-10-20 14:05:11 -07001382 kgsl_pwrscale_close(device);
1383
1384 adreno_dispatcher_close(adreno_dev);
1385 adreno_ringbuffer_close(adreno_dev);
1386
1387 adreno_fault_detect_stop(adreno_dev);
1388
1389 kfree(adreno_ft_regs);
1390 adreno_ft_regs = NULL;
1391
1392 kfree(adreno_ft_regs_val);
1393 adreno_ft_regs_val = NULL;
1394
1395 if (efuse_base != NULL)
1396 iounmap(efuse_base);
1397
1398 adreno_perfcounter_close(adreno_dev);
1399 kgsl_device_platform_remove(device);
1400
Kyle Pieferb1027b02017-02-10 13:58:58 -08001401 gmu_remove(device);
1402
Shrenuj Bansala419c792016-10-20 14:05:11 -07001403 if (test_bit(ADRENO_DEVICE_PWRON_FIXUP, &adreno_dev->priv)) {
1404 kgsl_free_global(device, &adreno_dev->pwron_fixup);
1405 clear_bit(ADRENO_DEVICE_PWRON_FIXUP, &adreno_dev->priv);
1406 }
1407 clear_bit(ADRENO_DEVICE_INITIALIZED, &adreno_dev->priv);
1408
1409 return 0;
1410}
1411
1412static void adreno_fault_detect_init(struct adreno_device *adreno_dev)
1413{
1414 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
Shrenuj Bansalae672812016-02-24 14:17:30 -08001415 int i;
1416
1417 if (!(swfdetect ||
1418 ADRENO_FEATURE(adreno_dev, ADRENO_SOFT_FAULT_DETECT)))
1419 return;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001420
1421 /* Disable the fast hang detect bit until we know its a go */
1422 adreno_dev->fast_hang_detect = 0;
1423
1424 adreno_ft_regs_num = (ARRAY_SIZE(adreno_ft_regs_default) +
1425 gpudev->ft_perf_counters_count*2);
1426
1427 adreno_ft_regs = kcalloc(adreno_ft_regs_num, sizeof(unsigned int),
1428 GFP_KERNEL);
1429 adreno_ft_regs_val = kcalloc(adreno_ft_regs_num, sizeof(unsigned int),
1430 GFP_KERNEL);
1431
1432 if (adreno_ft_regs == NULL || adreno_ft_regs_val == NULL) {
1433 kfree(adreno_ft_regs);
1434 kfree(adreno_ft_regs_val);
1435
1436 adreno_ft_regs = NULL;
1437 adreno_ft_regs_val = NULL;
1438
1439 return;
1440 }
1441
1442 for (i = 0; i < ARRAY_SIZE(adreno_ft_regs_default); i++)
1443 adreno_ft_regs[i] = adreno_getreg(adreno_dev,
1444 adreno_ft_regs_default[i]);
1445
1446 set_bit(ADRENO_DEVICE_SOFT_FAULT_DETECT, &adreno_dev->priv);
1447
Shrenuj Bansalae672812016-02-24 14:17:30 -08001448 adreno_fault_detect_start(adreno_dev);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001449}
1450
1451static int adreno_init(struct kgsl_device *device)
1452{
1453 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1454 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1455 int ret;
1456
Hareesh Gundu6aae7e22017-08-22 18:55:50 +05301457 if (!adreno_is_a3xx(adreno_dev))
1458 kgsl_sharedmem_set(device, &device->scratch, 0, 0,
1459 device->scratch.size);
1460
Shrenuj Bansala419c792016-10-20 14:05:11 -07001461 ret = kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
1462 if (ret)
1463 return ret;
1464
1465 /*
1466 * initialization only needs to be done once initially until
1467 * device is shutdown
1468 */
1469 if (test_bit(ADRENO_DEVICE_INITIALIZED, &adreno_dev->priv))
1470 return 0;
1471
1472 /*
1473 * Either the microcode read failed because the usermodehelper isn't
1474 * available or the microcode was corrupted. Fail the init and force
1475 * the user to try the open() again
1476 */
1477
1478 ret = gpudev->microcode_read(adreno_dev);
1479 if (ret)
1480 return ret;
1481
1482 /* Put the GPU in a responsive state */
George Shen3726c812017-05-12 11:06:03 -07001483 if (ADRENO_GPUREV(adreno_dev) < 600) {
1484 /* No need for newer generation architectures */
1485 ret = kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE);
1486 if (ret)
1487 return ret;
1488 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07001489
1490 ret = adreno_iommu_init(adreno_dev);
1491 if (ret)
1492 return ret;
1493
1494 adreno_perfcounter_init(adreno_dev);
1495 adreno_fault_detect_init(adreno_dev);
1496
1497 /* Power down the device */
George Shen3726c812017-05-12 11:06:03 -07001498 if (ADRENO_GPUREV(adreno_dev) < 600)
1499 kgsl_pwrctrl_change_state(device, KGSL_STATE_SLUMBER);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001500
1501 if (gpudev->init != NULL)
1502 gpudev->init(adreno_dev);
1503
1504 set_bit(ADRENO_DEVICE_INITIALIZED, &adreno_dev->priv);
1505
1506 /* Use shader offset and length defined in gpudev */
1507 if (adreno_dev->gpucore->shader_offset &&
1508 adreno_dev->gpucore->shader_size) {
1509
1510 if (device->shader_mem_phys || device->shader_mem_virt)
1511 KGSL_DRV_ERR(device,
1512 "Shader memory already specified in device tree\n");
1513 else {
1514 device->shader_mem_phys = device->reg_phys +
1515 adreno_dev->gpucore->shader_offset;
1516 device->shader_mem_virt = device->reg_virt +
1517 adreno_dev->gpucore->shader_offset;
1518 device->shader_mem_len =
1519 adreno_dev->gpucore->shader_size;
1520 }
1521 }
1522
1523 /*
1524 * Allocate a small chunk of memory for precise drawobj profiling for
1525 * those targets that have the always on timer
1526 */
1527
1528 if (!adreno_is_a3xx(adreno_dev)) {
1529 int r = kgsl_allocate_global(device,
1530 &adreno_dev->profile_buffer, PAGE_SIZE,
1531 0, 0, "alwayson");
1532
1533 adreno_dev->profile_index = 0;
1534
1535 if (r == 0) {
1536 set_bit(ADRENO_DEVICE_DRAWOBJ_PROFILE,
1537 &adreno_dev->priv);
1538 kgsl_sharedmem_set(device,
1539 &adreno_dev->profile_buffer, 0, 0,
1540 PAGE_SIZE);
1541 }
1542
1543 }
1544
Harshdeep Dhatt7ee8a862017-11-20 17:51:54 -07001545 if (nopreempt == false &&
1546 ADRENO_FEATURE(adreno_dev, ADRENO_PREEMPTION)) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07001547 int r = 0;
1548
1549 if (gpudev->preemption_init)
1550 r = gpudev->preemption_init(adreno_dev);
1551
1552 if (r == 0)
1553 set_bit(ADRENO_DEVICE_PREEMPTION, &adreno_dev->priv);
1554 else
1555 WARN(1, "adreno: GPU preemption is disabled\n");
1556 }
1557
1558 return 0;
1559}
1560
1561static bool regulators_left_on(struct kgsl_device *device)
1562{
1563 int i;
1564
George Shen3726c812017-05-12 11:06:03 -07001565 if (kgsl_gmu_isenabled(device))
1566 return false;
1567
Shrenuj Bansala419c792016-10-20 14:05:11 -07001568 for (i = 0; i < KGSL_MAX_REGULATORS; i++) {
1569 struct kgsl_regulator *regulator =
1570 &device->pwrctrl.regulators[i];
1571
1572 if (IS_ERR_OR_NULL(regulator->reg))
1573 break;
1574
1575 if (regulator_is_enabled(regulator->reg))
1576 return true;
1577 }
1578
1579 return false;
1580}
1581
1582static void _set_secvid(struct kgsl_device *device)
1583{
1584 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Carter Cooper6682ead2017-09-28 14:52:53 -06001585 static bool set;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001586
1587 /* Program GPU contect protection init values */
Carter Cooper6682ead2017-09-28 14:52:53 -06001588 if (device->mmu.secured && !set) {
Shrenuj Bansala419c792016-10-20 14:05:11 -07001589 if (adreno_is_a4xx(adreno_dev))
1590 adreno_writereg(adreno_dev,
1591 ADRENO_REG_RBBM_SECVID_TRUST_CONFIG, 0x2);
1592 adreno_writereg(adreno_dev,
1593 ADRENO_REG_RBBM_SECVID_TSB_CONTROL, 0x0);
1594
1595 adreno_writereg64(adreno_dev,
1596 ADRENO_REG_RBBM_SECVID_TSB_TRUSTED_BASE,
1597 ADRENO_REG_RBBM_SECVID_TSB_TRUSTED_BASE_HI,
Deepak Kumar756d6a92017-11-28 16:58:29 +05301598 KGSL_IOMMU_SECURE_BASE(&device->mmu));
Shrenuj Bansala419c792016-10-20 14:05:11 -07001599 adreno_writereg(adreno_dev,
1600 ADRENO_REG_RBBM_SECVID_TSB_TRUSTED_SIZE,
1601 KGSL_IOMMU_SECURE_SIZE);
Carter Cooper6682ead2017-09-28 14:52:53 -06001602 if (ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_SECVID_SET_ONCE))
1603 set = true;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001604 }
1605}
1606
Carter Cooper1d8f5472017-03-15 15:01:09 -06001607static int adreno_switch_to_unsecure_mode(struct adreno_device *adreno_dev,
1608 struct adreno_ringbuffer *rb)
1609{
1610 unsigned int *cmds;
1611 int ret;
1612
1613 cmds = adreno_ringbuffer_allocspace(rb, 2);
1614 if (IS_ERR(cmds))
1615 return PTR_ERR(cmds);
1616 if (cmds == NULL)
1617 return -ENOSPC;
1618
1619 cmds += cp_secure_mode(adreno_dev, cmds, 0);
1620
1621 ret = adreno_ringbuffer_submit_spin(rb, NULL, 2000);
1622 if (ret)
1623 adreno_spin_idle_debug(adreno_dev,
1624 "Switch to unsecure failed to idle\n");
1625
1626 return ret;
1627}
1628
1629int adreno_set_unsecured_mode(struct adreno_device *adreno_dev,
1630 struct adreno_ringbuffer *rb)
1631{
1632 int ret = 0;
1633
Carter Cooper4a313ae2017-02-23 11:11:56 -07001634 if (!adreno_is_a5xx(adreno_dev) && !adreno_is_a6xx(adreno_dev))
Carter Cooper1d8f5472017-03-15 15:01:09 -06001635 return -EINVAL;
1636
1637 if (ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_CRITICAL_PACKETS) &&
1638 adreno_is_a5xx(adreno_dev)) {
1639 ret = a5xx_critical_packet_submit(adreno_dev, rb);
1640 if (ret)
1641 return ret;
1642 }
1643
1644 /* GPU comes up in secured mode, make it unsecured by default */
Harshdeep Dhatta9e0d762017-05-10 14:16:42 -06001645 if (adreno_dev->zap_loaded)
Carter Cooper1d8f5472017-03-15 15:01:09 -06001646 ret = adreno_switch_to_unsecure_mode(adreno_dev, rb);
1647 else
1648 adreno_writereg(adreno_dev,
1649 ADRENO_REG_RBBM_SECVID_TRUST_CONTROL, 0x0);
1650
1651 return ret;
1652}
1653
Lynus Vaze1fabf12017-10-09 21:33:26 +05301654static void adreno_set_active_ctxs_null(struct adreno_device *adreno_dev)
1655{
1656 int i;
1657 struct adreno_ringbuffer *rb;
1658
1659 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
1660 if (rb->drawctxt_active)
1661 kgsl_context_put(&(rb->drawctxt_active->base));
1662 rb->drawctxt_active = NULL;
1663
1664 kgsl_sharedmem_writel(KGSL_DEVICE(adreno_dev),
1665 &rb->pagetable_desc, PT_INFO_OFFSET(current_rb_ptname),
1666 0);
1667 }
1668}
1669
Shrenuj Bansala419c792016-10-20 14:05:11 -07001670/**
1671 * _adreno_start - Power up the GPU and prepare to accept commands
1672 * @adreno_dev: Pointer to an adreno_device structure
1673 *
1674 * The core function that powers up and initalizes the GPU. This function is
1675 * called at init and after coming out of SLUMBER
1676 */
1677static int _adreno_start(struct adreno_device *adreno_dev)
1678{
1679 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
1680 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
1681 int status = -EINVAL, ret;
1682 unsigned int state = device->state;
1683 bool regulator_left_on;
1684 unsigned int pmqos_wakeup_vote = device->pwrctrl.pm_qos_wakeup_latency;
1685 unsigned int pmqos_active_vote = device->pwrctrl.pm_qos_active_latency;
1686
1687 /* make sure ADRENO_DEVICE_STARTED is not set here */
1688 BUG_ON(test_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv));
1689
Gaurav Sonwanic169c322017-06-15 14:11:23 +05301690 /* disallow l2pc during wake up to improve GPU wake up time */
1691 kgsl_pwrctrl_update_l2pc(&adreno_dev->dev,
1692 KGSL_L2PC_WAKEUP_TIMEOUT);
1693
Shrenuj Bansala419c792016-10-20 14:05:11 -07001694 pm_qos_update_request(&device->pwrctrl.pm_qos_req_dma,
1695 pmqos_wakeup_vote);
1696
1697 regulator_left_on = regulators_left_on(device);
1698
1699 /* Clear any GPU faults that might have been left over */
1700 adreno_clear_gpu_fault(adreno_dev);
1701
1702 /* Put the GPU in a responsive state */
1703 status = kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE);
1704 if (status)
1705 goto error_pwr_off;
1706
Lynus Vaze1fabf12017-10-09 21:33:26 +05301707 /* Set any stale active contexts to NULL */
1708 adreno_set_active_ctxs_null(adreno_dev);
1709
Shrenuj Bansala419c792016-10-20 14:05:11 -07001710 /* Set the bit to indicate that we've just powered on */
1711 set_bit(ADRENO_DEVICE_PWRON, &adreno_dev->priv);
1712
1713 /* Soft reset the GPU if a regulator is stuck on*/
1714 if (regulator_left_on)
1715 _soft_reset(adreno_dev);
1716
1717 adreno_ringbuffer_set_global(adreno_dev, 0);
1718
1719 status = kgsl_mmu_start(device);
1720 if (status)
1721 goto error_pwr_off;
1722
1723 _set_secvid(device);
1724
1725 status = adreno_ocmem_malloc(adreno_dev);
1726 if (status) {
1727 KGSL_DRV_ERR(device, "OCMEM malloc failed\n");
1728 goto error_mmu_off;
1729 }
1730
Carter Coopera2a12982017-05-02 08:43:15 -06001731 /* Send OOB request to turn on the GX */
1732 if (gpudev->oob_set) {
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001733 status = gpudev->oob_set(adreno_dev, OOB_GPU_SET_MASK,
1734 OOB_GPU_CHECK_MASK,
1735 OOB_GPU_CLEAR_MASK);
Carter Coopera2a12982017-05-02 08:43:15 -06001736 if (status)
1737 goto error_mmu_off;
1738 }
1739
Shrenuj Bansala419c792016-10-20 14:05:11 -07001740 /* Enable 64 bit gpu addr if feature is set */
1741 if (gpudev->enable_64bit &&
1742 adreno_support_64bit(adreno_dev))
1743 gpudev->enable_64bit(adreno_dev);
1744
1745 if (adreno_dev->perfctr_pwr_lo == 0) {
1746 ret = adreno_perfcounter_get(adreno_dev,
1747 KGSL_PERFCOUNTER_GROUP_PWR, 1,
1748 &adreno_dev->perfctr_pwr_lo, NULL,
1749 PERFCOUNTER_FLAG_KERNEL);
1750
1751 if (ret) {
Kyle Piefer74645b532017-05-16 11:45:40 -07001752 WARN_ONCE(1, "Unable to get perf counters for DCVS\n");
Shrenuj Bansala419c792016-10-20 14:05:11 -07001753 adreno_dev->perfctr_pwr_lo = 0;
1754 }
1755 }
1756
1757
1758 if (device->pwrctrl.bus_control) {
1759 /* VBIF waiting for RAM */
1760 if (adreno_dev->starved_ram_lo == 0) {
1761 ret = adreno_perfcounter_get(adreno_dev,
1762 KGSL_PERFCOUNTER_GROUP_VBIF_PWR, 0,
1763 &adreno_dev->starved_ram_lo, NULL,
1764 PERFCOUNTER_FLAG_KERNEL);
1765
1766 if (ret) {
1767 KGSL_DRV_ERR(device,
1768 "Unable to get perf counters for bus DCVS\n");
1769 adreno_dev->starved_ram_lo = 0;
1770 }
1771 }
1772
Deepak Kumarc52781f2017-11-06 16:10:17 +05301773 if (adreno_has_gbif(adreno_dev)) {
1774 if (adreno_dev->starved_ram_lo_ch1 == 0) {
1775 ret = adreno_perfcounter_get(adreno_dev,
1776 KGSL_PERFCOUNTER_GROUP_VBIF_PWR, 1,
1777 &adreno_dev->starved_ram_lo_ch1, NULL,
1778 PERFCOUNTER_FLAG_KERNEL);
1779
1780 if (ret) {
1781 KGSL_DRV_ERR(device,
1782 "Unable to get perf counters for bus DCVS\n");
1783 adreno_dev->starved_ram_lo_ch1 = 0;
1784 }
1785 }
Deepak Kumarc52781f2017-11-06 16:10:17 +05301786
Deepak Kumar84b9e032017-11-08 13:08:50 +05301787 if (adreno_dev->ram_cycles_lo == 0) {
1788 ret = adreno_perfcounter_get(adreno_dev,
1789 KGSL_PERFCOUNTER_GROUP_VBIF,
1790 GBIF_AXI0_READ_DATA_TOTAL_BEATS,
1791 &adreno_dev->ram_cycles_lo, NULL,
1792 PERFCOUNTER_FLAG_KERNEL);
Shrenuj Bansala419c792016-10-20 14:05:11 -07001793
Deepak Kumar84b9e032017-11-08 13:08:50 +05301794 if (ret) {
1795 KGSL_DRV_ERR(device,
1796 "Unable to get perf counters for bus DCVS\n");
1797 adreno_dev->ram_cycles_lo = 0;
1798 }
1799 }
1800
1801 if (adreno_dev->ram_cycles_lo_ch1_read == 0) {
1802 ret = adreno_perfcounter_get(adreno_dev,
1803 KGSL_PERFCOUNTER_GROUP_VBIF,
1804 GBIF_AXI1_READ_DATA_TOTAL_BEATS,
1805 &adreno_dev->ram_cycles_lo_ch1_read,
1806 NULL,
1807 PERFCOUNTER_FLAG_KERNEL);
1808
1809 if (ret) {
1810 KGSL_DRV_ERR(device,
1811 "Unable to get perf counters for bus DCVS\n");
1812 adreno_dev->ram_cycles_lo_ch1_read = 0;
1813 }
1814 }
1815
1816 if (adreno_dev->ram_cycles_lo_ch0_write == 0) {
1817 ret = adreno_perfcounter_get(adreno_dev,
1818 KGSL_PERFCOUNTER_GROUP_VBIF,
1819 GBIF_AXI0_WRITE_DATA_TOTAL_BEATS,
1820 &adreno_dev->ram_cycles_lo_ch0_write,
1821 NULL,
1822 PERFCOUNTER_FLAG_KERNEL);
1823
1824 if (ret) {
1825 KGSL_DRV_ERR(device,
1826 "Unable to get perf counters for bus DCVS\n");
1827 adreno_dev->ram_cycles_lo_ch0_write = 0;
1828 }
1829 }
1830
1831 if (adreno_dev->ram_cycles_lo_ch1_write == 0) {
1832 ret = adreno_perfcounter_get(adreno_dev,
1833 KGSL_PERFCOUNTER_GROUP_VBIF,
1834 GBIF_AXI1_WRITE_DATA_TOTAL_BEATS,
1835 &adreno_dev->ram_cycles_lo_ch1_write,
1836 NULL,
1837 PERFCOUNTER_FLAG_KERNEL);
1838
1839 if (ret) {
1840 KGSL_DRV_ERR(device,
1841 "Unable to get perf counters for bus DCVS\n");
1842 adreno_dev->ram_cycles_lo_ch1_write = 0;
1843 }
1844 }
1845 } else {
1846 /* VBIF DDR cycles */
1847 if (adreno_dev->ram_cycles_lo == 0) {
1848 ret = adreno_perfcounter_get(adreno_dev,
1849 KGSL_PERFCOUNTER_GROUP_VBIF,
1850 VBIF_AXI_TOTAL_BEATS,
1851 &adreno_dev->ram_cycles_lo, NULL,
1852 PERFCOUNTER_FLAG_KERNEL);
1853
1854 if (ret) {
1855 KGSL_DRV_ERR(device,
1856 "Unable to get perf counters for bus DCVS\n");
1857 adreno_dev->ram_cycles_lo = 0;
1858 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07001859 }
1860 }
1861 }
1862
1863 /* Clear the busy_data stats - we're starting over from scratch */
1864 adreno_dev->busy_data.gpu_busy = 0;
Deepak Kumar84b9e032017-11-08 13:08:50 +05301865 adreno_dev->busy_data.bif_ram_cycles = 0;
1866 adreno_dev->busy_data.bif_ram_cycles_read_ch1 = 0;
1867 adreno_dev->busy_data.bif_ram_cycles_write_ch0 = 0;
1868 adreno_dev->busy_data.bif_ram_cycles_write_ch1 = 0;
1869 adreno_dev->busy_data.bif_starved_ram = 0;
1870 adreno_dev->busy_data.bif_starved_ram_ch1 = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001871
1872 /* Restore performance counter registers with saved values */
1873 adreno_perfcounter_restore(adreno_dev);
1874
1875 /* Start the GPU */
1876 gpudev->start(adreno_dev);
1877
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06001878 /*
1879 * The system cache control registers
1880 * live on the CX rail. Hence need
1881 * reprogramming everytime the GPU
1882 * comes out of power collapse.
1883 */
1884 adreno_llc_setup(device);
1885
Shrenuj Bansala419c792016-10-20 14:05:11 -07001886 /* Re-initialize the coresight registers if applicable */
1887 adreno_coresight_start(adreno_dev);
1888
1889 adreno_irqctrl(adreno_dev, 1);
1890
1891 adreno_perfcounter_start(adreno_dev);
1892
1893 /* Clear FSR here in case it is set from a previous pagefault */
1894 kgsl_mmu_clear_fsr(&device->mmu);
1895
1896 status = adreno_ringbuffer_start(adreno_dev, ADRENO_START_COLD);
1897 if (status)
Carter Coopera2a12982017-05-02 08:43:15 -06001898 goto error_oob_clear;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001899
1900 /* Start the dispatcher */
1901 adreno_dispatcher_start(device);
1902
1903 device->reset_counter++;
1904
1905 set_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);
1906
1907 if (pmqos_active_vote != pmqos_wakeup_vote)
1908 pm_qos_update_request(&device->pwrctrl.pm_qos_req_dma,
1909 pmqos_active_vote);
1910
Carter Coopera2a12982017-05-02 08:43:15 -06001911 /* Send OOB request to allow IFPC */
Kyle Piefer83656c82017-09-11 14:23:37 -07001912 if (gpudev->oob_clear) {
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001913 gpudev->oob_clear(adreno_dev, OOB_GPU_CLEAR_MASK);
Carter Coopera2a12982017-05-02 08:43:15 -06001914
Kyle Piefer83656c82017-09-11 14:23:37 -07001915 /* If we made it this far, the BOOT OOB was sent to the GMU */
1916 if (ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_HFI_USE_REG))
1917 gpudev->oob_clear(adreno_dev,
1918 OOB_BOOT_SLUMBER_CLEAR_MASK);
1919 }
1920
Shrenuj Bansala419c792016-10-20 14:05:11 -07001921 return 0;
1922
Carter Coopera2a12982017-05-02 08:43:15 -06001923error_oob_clear:
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001924 if (gpudev->oob_clear)
1925 gpudev->oob_clear(adreno_dev, OOB_GPU_CLEAR_MASK);
Carter Coopera2a12982017-05-02 08:43:15 -06001926
Shrenuj Bansala419c792016-10-20 14:05:11 -07001927error_mmu_off:
1928 kgsl_mmu_stop(&device->mmu);
1929
1930error_pwr_off:
1931 /* set the state back to original state */
1932 kgsl_pwrctrl_change_state(device, state);
1933
1934 if (pmqos_active_vote != pmqos_wakeup_vote)
1935 pm_qos_update_request(&device->pwrctrl.pm_qos_req_dma,
1936 pmqos_active_vote);
1937
1938 return status;
1939}
1940
1941/**
1942 * adreno_start() - Power up and initialize the GPU
1943 * @device: Pointer to the KGSL device to power up
1944 * @priority: Boolean flag to specify of the start should be scheduled in a low
1945 * latency work queue
1946 *
1947 * Power up the GPU and initialize it. If priority is specified then elevate
1948 * the thread priority for the duration of the start operation
1949 */
Shrenuj Bansald0fe7462017-05-08 16:11:19 -07001950int adreno_start(struct kgsl_device *device, int priority)
Shrenuj Bansala419c792016-10-20 14:05:11 -07001951{
1952 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
1953 int nice = task_nice(current);
1954 int ret;
1955
1956 if (priority && (adreno_wake_nice < nice))
1957 set_user_nice(current, adreno_wake_nice);
1958
1959 ret = _adreno_start(adreno_dev);
1960
1961 if (priority)
1962 set_user_nice(current, nice);
1963
1964 return ret;
1965}
1966
Shrenuj Bansala419c792016-10-20 14:05:11 -07001967static int adreno_stop(struct kgsl_device *device)
1968{
1969 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
Kyle Piefer4033f562017-08-16 10:00:48 -07001970 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001971 int error = 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07001972
1973 if (!test_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv))
1974 return 0;
1975
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001976 /* Turn the power on one last time before stopping */
1977 if (gpudev->oob_set) {
1978 error = gpudev->oob_set(adreno_dev, OOB_GPU_SET_MASK,
1979 OOB_GPU_CHECK_MASK,
1980 OOB_GPU_CLEAR_MASK);
1981 if (error) {
George Shenb95afc42017-12-05 14:56:27 -08001982 struct gmu_device *gmu = &device->gmu;
1983
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001984 gpudev->oob_clear(adreno_dev, OOB_GPU_CLEAR_MASK);
George Shenb95afc42017-12-05 14:56:27 -08001985 if (gmu->gx_gdsc &&
1986 regulator_is_enabled(gmu->gx_gdsc)) {
1987 /* GPU is on. Try recovery */
1988 set_bit(GMU_FAULT, &gmu->flags);
1989 gmu_snapshot(device);
1990 error = -EINVAL;
1991 } else {
1992 return error;
1993 }
Kyle Piefer8fe58df2017-09-12 09:19:28 -07001994 }
1995 }
1996
Shrenuj Bansala419c792016-10-20 14:05:11 -07001997 adreno_dispatcher_stop(adreno_dev);
1998
1999 adreno_ringbuffer_stop(adreno_dev);
2000
2001 kgsl_pwrscale_update_stats(device);
2002
2003 adreno_irqctrl(adreno_dev, 0);
2004
2005 adreno_ocmem_free(adreno_dev);
2006
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06002007 if (adreno_dev->gpu_llc_slice)
2008 adreno_llc_deactivate_slice(adreno_dev->gpu_llc_slice);
Sushmita Susheelendra906564d2017-01-10 15:53:55 -07002009 if (adreno_dev->gpuhtw_llc_slice)
2010 adreno_llc_deactivate_slice(adreno_dev->gpuhtw_llc_slice);
Sushmita Susheelendra7f66cf72016-09-12 11:04:43 -06002011
Shrenuj Bansala419c792016-10-20 14:05:11 -07002012 /* Save active coresight registers if applicable */
2013 adreno_coresight_stop(adreno_dev);
2014
2015 /* Save physical performance counter values before GPU power down*/
2016 adreno_perfcounter_save(adreno_dev);
2017
Kyle Piefer8fe58df2017-09-12 09:19:28 -07002018 if (gpudev->oob_clear)
2019 gpudev->oob_clear(adreno_dev, OOB_GPU_CLEAR_MASK);
2020
Kyle Piefer4033f562017-08-16 10:00:48 -07002021 /*
2022 * Saving perfcounters will use an OOB to put the GMU into
2023 * active state. Before continuing, we should wait for the
2024 * GMU to return to the lowest idle level. This is
2025 * because some idle level transitions require VBIF and MMU.
2026 */
George Shenb95afc42017-12-05 14:56:27 -08002027 if (!error && gpudev->wait_for_lowest_idle &&
George Shenf6c15bd2017-11-01 12:22:12 -07002028 gpudev->wait_for_lowest_idle(adreno_dev)) {
2029 struct gmu_device *gmu = &device->gmu;
2030
2031 set_bit(GMU_FAULT, &gmu->flags);
2032 gmu_snapshot(device);
2033 /*
2034 * Assume GMU hang after 10ms without responding.
2035 * It shall be relative safe to clear vbif and stop
2036 * MMU later. Early return in adreno_stop function
2037 * will result in kernel panic in adreno_start
2038 */
2039 error = -EINVAL;
2040 }
Kyle Piefer4033f562017-08-16 10:00:48 -07002041
Shrenuj Bansala419c792016-10-20 14:05:11 -07002042 adreno_vbif_clear_pending_transactions(device);
2043
2044 kgsl_mmu_stop(&device->mmu);
2045
Harshdeep Dhatt6342e6b2017-09-21 21:25:21 -06002046 /*
2047 * At this point, MMU is turned off so we can safely
2048 * destroy any pending contexts and their pagetables
2049 */
2050 adreno_set_active_ctxs_null(adreno_dev);
2051
Shrenuj Bansala419c792016-10-20 14:05:11 -07002052 clear_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);
2053
George Shenf6c15bd2017-11-01 12:22:12 -07002054 return error;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002055}
2056
2057static inline bool adreno_try_soft_reset(struct kgsl_device *device, int fault)
2058{
2059 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2060
2061 /*
2062 * Do not do soft reset for a IOMMU fault (because the IOMMU hardware
2063 * needs a reset too) or for the A304 because it can't do SMMU
2064 * programming of any kind after a soft reset
2065 */
2066
2067 if ((fault & ADRENO_IOMMU_PAGE_FAULT) || adreno_is_a304(adreno_dev))
2068 return false;
2069
2070 return true;
2071}
2072
2073/**
2074 * adreno_reset() - Helper function to reset the GPU
2075 * @device: Pointer to the KGSL device structure for the GPU
2076 * @fault: Type of fault. Needed to skip soft reset for MMU fault
2077 *
2078 * Try to reset the GPU to recover from a fault. First, try to do a low latency
2079 * soft reset. If the soft reset fails for some reason, then bring out the big
2080 * guns and toggle the footswitch.
2081 */
2082int adreno_reset(struct kgsl_device *device, int fault)
2083{
2084 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2085 int ret = -EINVAL;
2086 int i = 0;
2087
2088 /* Try soft reset first */
2089 if (adreno_try_soft_reset(device, fault)) {
2090 /* Make sure VBIF is cleared before resetting */
2091 ret = adreno_vbif_clear_pending_transactions(device);
2092
2093 if (ret == 0) {
2094 ret = adreno_soft_reset(device);
2095 if (ret)
2096 KGSL_DEV_ERR_ONCE(device,
2097 "Device soft reset failed\n");
2098 }
2099 }
2100 if (ret) {
2101 /* If soft reset failed/skipped, then pull the power */
2102 kgsl_pwrctrl_change_state(device, KGSL_STATE_INIT);
2103 /* since device is officially off now clear start bit */
2104 clear_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);
2105
2106 /* Keep trying to start the device until it works */
2107 for (i = 0; i < NUM_TIMES_RESET_RETRY; i++) {
2108 ret = adreno_start(device, 0);
2109 if (!ret)
2110 break;
2111
2112 msleep(20);
2113 }
2114 }
2115 if (ret)
2116 return ret;
2117
2118 if (i != 0)
2119 KGSL_DRV_WARN(device, "Device hard reset tried %d tries\n", i);
2120
2121 /*
2122 * If active_cnt is non-zero then the system was active before
2123 * going into a reset - put it back in that state
2124 */
2125
2126 if (atomic_read(&device->active_cnt))
2127 kgsl_pwrctrl_change_state(device, KGSL_STATE_ACTIVE);
2128 else
2129 kgsl_pwrctrl_change_state(device, KGSL_STATE_NAP);
2130
2131 return ret;
2132}
2133
2134static int adreno_getproperty(struct kgsl_device *device,
2135 unsigned int type,
2136 void __user *value,
2137 size_t sizebytes)
2138{
2139 int status = -EINVAL;
2140 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2141
2142 switch (type) {
2143 case KGSL_PROP_DEVICE_INFO:
2144 {
2145 struct kgsl_devinfo devinfo;
2146
2147 if (sizebytes != sizeof(devinfo)) {
2148 status = -EINVAL;
2149 break;
2150 }
2151
2152 memset(&devinfo, 0, sizeof(devinfo));
2153 devinfo.device_id = device->id+1;
2154 devinfo.chip_id = adreno_dev->chipid;
2155 devinfo.mmu_enabled =
2156 MMU_FEATURE(&device->mmu, KGSL_MMU_PAGED);
2157 devinfo.gmem_gpubaseaddr = adreno_dev->gmem_base;
2158 devinfo.gmem_sizebytes = adreno_dev->gmem_size;
2159
2160 if (copy_to_user(value, &devinfo, sizeof(devinfo)) !=
2161 0) {
2162 status = -EFAULT;
2163 break;
2164 }
2165 status = 0;
2166 }
2167 break;
2168 case KGSL_PROP_DEVICE_SHADOW:
2169 {
2170 struct kgsl_shadowprop shadowprop;
2171
2172 if (sizebytes != sizeof(shadowprop)) {
2173 status = -EINVAL;
2174 break;
2175 }
2176 memset(&shadowprop, 0, sizeof(shadowprop));
2177 if (device->memstore.hostptr) {
2178 /*NOTE: with mmu enabled, gpuaddr doesn't mean
2179 * anything to mmap().
2180 */
2181 shadowprop.gpuaddr =
Deepak Kumar756d6a92017-11-28 16:58:29 +05302182 (unsigned long)device->memstore.gpuaddr;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002183 shadowprop.size = device->memstore.size;
2184 /* GSL needs this to be set, even if it
2185 * appears to be meaningless
2186 */
2187 shadowprop.flags = KGSL_FLAGS_INITIALIZED |
2188 KGSL_FLAGS_PER_CONTEXT_TIMESTAMPS;
2189 }
2190 if (copy_to_user(value, &shadowprop,
2191 sizeof(shadowprop))) {
2192 status = -EFAULT;
2193 break;
2194 }
2195 status = 0;
2196 }
2197 break;
2198 case KGSL_PROP_DEVICE_QDSS_STM:
2199 {
2200 struct kgsl_qdss_stm_prop qdssprop = {0};
2201 struct kgsl_memdesc *qdss_desc =
2202 kgsl_mmu_get_qdss_global_entry(device);
2203
2204 if (sizebytes != sizeof(qdssprop)) {
2205 status = -EINVAL;
2206 break;
2207 }
2208
2209 if (qdss_desc) {
2210 qdssprop.gpuaddr = qdss_desc->gpuaddr;
2211 qdssprop.size = qdss_desc->size;
2212 }
2213
2214 if (copy_to_user(value, &qdssprop,
2215 sizeof(qdssprop))) {
2216 status = -EFAULT;
2217 break;
2218 }
2219 status = 0;
2220 }
2221 break;
Jonathan Wicks4892d8d2017-02-24 16:21:26 -07002222 case KGSL_PROP_DEVICE_QTIMER:
2223 {
2224 struct kgsl_qtimer_prop qtimerprop = {0};
2225 struct kgsl_memdesc *qtimer_desc =
2226 kgsl_mmu_get_qtimer_global_entry(device);
2227
2228 if (sizebytes != sizeof(qtimerprop)) {
2229 status = -EINVAL;
2230 break;
2231 }
2232
2233 if (qtimer_desc) {
2234 qtimerprop.gpuaddr = qtimer_desc->gpuaddr;
2235 qtimerprop.size = qtimer_desc->size;
2236 }
2237
2238 if (copy_to_user(value, &qtimerprop,
2239 sizeof(qtimerprop))) {
2240 status = -EFAULT;
2241 break;
2242 }
2243 status = 0;
2244 }
2245 break;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002246 case KGSL_PROP_MMU_ENABLE:
2247 {
2248 /* Report MMU only if we can handle paged memory */
2249 int mmu_prop = MMU_FEATURE(&device->mmu,
2250 KGSL_MMU_PAGED);
2251
2252 if (sizebytes < sizeof(mmu_prop)) {
2253 status = -EINVAL;
2254 break;
2255 }
2256 if (copy_to_user(value, &mmu_prop, sizeof(mmu_prop))) {
2257 status = -EFAULT;
2258 break;
2259 }
2260 status = 0;
2261 }
2262 break;
2263 case KGSL_PROP_INTERRUPT_WAITS:
2264 {
2265 int int_waits = 1;
2266
2267 if (sizebytes != sizeof(int)) {
2268 status = -EINVAL;
2269 break;
2270 }
2271 if (copy_to_user(value, &int_waits, sizeof(int))) {
2272 status = -EFAULT;
2273 break;
2274 }
2275 status = 0;
2276 }
2277 break;
2278 case KGSL_PROP_UCHE_GMEM_VADDR:
2279 {
2280 uint64_t gmem_vaddr = 0;
2281
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -07002282 if (adreno_is_a5xx(adreno_dev) ||
2283 adreno_is_a6xx(adreno_dev))
Shrenuj Bansala419c792016-10-20 14:05:11 -07002284 gmem_vaddr = ADRENO_UCHE_GMEM_BASE;
2285 if (sizebytes != sizeof(uint64_t)) {
2286 status = -EINVAL;
2287 break;
2288 }
2289 if (copy_to_user(value, &gmem_vaddr,
2290 sizeof(uint64_t))) {
2291 status = -EFAULT;
2292 break;
2293 }
2294 status = 0;
2295 }
2296 break;
2297 case KGSL_PROP_SP_GENERIC_MEM:
2298 {
2299 struct kgsl_sp_generic_mem sp_mem;
2300
2301 if (sizebytes != sizeof(sp_mem)) {
2302 status = -EINVAL;
2303 break;
2304 }
2305 memset(&sp_mem, 0, sizeof(sp_mem));
2306
2307 sp_mem.local = adreno_dev->sp_local_gpuaddr;
2308 sp_mem.pvt = adreno_dev->sp_pvt_gpuaddr;
2309
2310 if (copy_to_user(value, &sp_mem, sizeof(sp_mem))) {
2311 status = -EFAULT;
2312 break;
2313 }
2314 status = 0;
2315 }
2316 break;
2317 case KGSL_PROP_UCODE_VERSION:
2318 {
2319 struct kgsl_ucode_version ucode;
2320
2321 if (sizebytes != sizeof(ucode)) {
2322 status = -EINVAL;
2323 break;
2324 }
2325 memset(&ucode, 0, sizeof(ucode));
2326
Shrenuj Bansalacf1ef42016-06-01 11:11:27 -07002327 ucode.pfp = adreno_dev->fw[ADRENO_FW_PFP].version;
2328 ucode.pm4 = adreno_dev->fw[ADRENO_FW_PM4].version;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002329
2330 if (copy_to_user(value, &ucode, sizeof(ucode))) {
2331 status = -EFAULT;
2332 break;
2333 }
2334 status = 0;
2335 }
2336 break;
2337 case KGSL_PROP_GPMU_VERSION:
2338 {
2339 struct kgsl_gpmu_version gpmu;
2340
2341 if (adreno_dev->gpucore == NULL) {
2342 status = -EINVAL;
2343 break;
2344 }
2345
2346 if (!ADRENO_FEATURE(adreno_dev, ADRENO_GPMU)) {
2347 status = -EOPNOTSUPP;
2348 break;
2349 }
2350
2351 if (sizebytes != sizeof(gpmu)) {
2352 status = -EINVAL;
2353 break;
2354 }
2355 memset(&gpmu, 0, sizeof(gpmu));
2356
2357 gpmu.major = adreno_dev->gpucore->gpmu_major;
2358 gpmu.minor = adreno_dev->gpucore->gpmu_minor;
2359 gpmu.features = adreno_dev->gpucore->gpmu_features;
2360
2361 if (copy_to_user(value, &gpmu, sizeof(gpmu))) {
2362 status = -EFAULT;
2363 break;
2364 }
2365 status = 0;
2366 }
2367 break;
2368 case KGSL_PROP_HIGHEST_BANK_BIT:
2369 {
2370 unsigned int bit;
2371
2372 if (sizebytes < sizeof(unsigned int)) {
2373 status = -EINVAL;
2374 break;
2375 }
2376
2377 if (of_property_read_u32(device->pdev->dev.of_node,
2378 "qcom,highest-bank-bit", &bit)) {
2379 status = -EINVAL;
2380 break;
2381 }
2382
2383 if (copy_to_user(value, &bit, sizeof(bit))) {
2384 status = -EFAULT;
2385 break;
2386 }
2387 }
2388 status = 0;
2389 break;
Shrenuj Bansala9ae9de2016-11-15 16:01:00 -08002390 case KGSL_PROP_MIN_ACCESS_LENGTH:
2391 {
2392 unsigned int mal;
2393
2394 if (sizebytes < sizeof(unsigned int)) {
2395 status = -EINVAL;
2396 break;
2397 }
2398
2399 if (of_property_read_u32(device->pdev->dev.of_node,
2400 "qcom,min-access-length", &mal)) {
2401 mal = 0;
2402 }
2403
2404 if (copy_to_user(value, &mal, sizeof(mal))) {
2405 status = -EFAULT;
2406 break;
2407 }
2408 }
2409 status = 0;
2410 break;
2411 case KGSL_PROP_UBWC_MODE:
2412 {
2413 unsigned int mode;
2414
2415 if (sizebytes < sizeof(unsigned int)) {
2416 status = -EINVAL;
2417 break;
2418 }
2419
2420 if (of_property_read_u32(device->pdev->dev.of_node,
2421 "qcom,ubwc-mode", &mode))
2422 mode = 0;
2423
2424 if (copy_to_user(value, &mode, sizeof(mode))) {
2425 status = -EFAULT;
2426 break;
2427 }
2428 }
2429 status = 0;
2430 break;
2431
Shrenuj Bansala419c792016-10-20 14:05:11 -07002432 case KGSL_PROP_DEVICE_BITNESS:
2433 {
2434 unsigned int bitness = 32;
2435
2436 if (sizebytes != sizeof(unsigned int)) {
2437 status = -EINVAL;
2438 break;
2439 }
2440 /* No of bits used by the GPU */
2441 if (adreno_support_64bit(adreno_dev))
2442 bitness = 48;
2443
2444 if (copy_to_user(value, &bitness,
2445 sizeof(unsigned int))) {
2446 status = -EFAULT;
2447 break;
2448 }
2449 status = 0;
2450 }
2451 break;
2452
2453 default:
2454 status = -EINVAL;
2455 }
2456
2457 return status;
2458}
2459
2460int adreno_set_constraint(struct kgsl_device *device,
2461 struct kgsl_context *context,
2462 struct kgsl_device_constraint *constraint)
2463{
2464 int status = 0;
2465
2466 switch (constraint->type) {
2467 case KGSL_CONSTRAINT_PWRLEVEL: {
2468 struct kgsl_device_constraint_pwrlevel pwr;
2469
2470 if (constraint->size != sizeof(pwr)) {
2471 status = -EINVAL;
2472 break;
2473 }
2474
2475 if (copy_from_user(&pwr,
2476 (void __user *)constraint->data,
2477 sizeof(pwr))) {
2478 status = -EFAULT;
2479 break;
2480 }
2481 if (pwr.level >= KGSL_CONSTRAINT_PWR_MAXLEVELS) {
2482 status = -EINVAL;
2483 break;
2484 }
2485
2486 context->pwr_constraint.type =
2487 KGSL_CONSTRAINT_PWRLEVEL;
2488 context->pwr_constraint.sub_type = pwr.level;
2489 trace_kgsl_user_pwrlevel_constraint(device,
2490 context->id,
2491 context->pwr_constraint.type,
2492 context->pwr_constraint.sub_type);
2493 }
2494 break;
2495 case KGSL_CONSTRAINT_NONE:
2496 if (context->pwr_constraint.type == KGSL_CONSTRAINT_PWRLEVEL)
2497 trace_kgsl_user_pwrlevel_constraint(device,
2498 context->id,
2499 KGSL_CONSTRAINT_NONE,
2500 context->pwr_constraint.sub_type);
2501 context->pwr_constraint.type = KGSL_CONSTRAINT_NONE;
2502 break;
2503
2504 default:
2505 status = -EINVAL;
2506 break;
2507 }
2508
2509 /* If a new constraint has been set for a context, cancel the old one */
2510 if ((status == 0) &&
2511 (context->id == device->pwrctrl.constraint.owner_id)) {
2512 trace_kgsl_constraint(device, device->pwrctrl.constraint.type,
2513 device->pwrctrl.active_pwrlevel, 0);
2514 device->pwrctrl.constraint.type = KGSL_CONSTRAINT_NONE;
2515 }
2516
2517 return status;
2518}
2519
2520static int adreno_setproperty(struct kgsl_device_private *dev_priv,
2521 unsigned int type,
2522 void __user *value,
2523 unsigned int sizebytes)
2524{
2525 int status = -EINVAL;
2526 struct kgsl_device *device = dev_priv->device;
2527 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2528
2529 switch (type) {
2530 case KGSL_PROP_PWRCTRL: {
2531 unsigned int enable;
2532
2533 if (sizebytes != sizeof(enable))
2534 break;
2535
2536 if (copy_from_user(&enable, value, sizeof(enable))) {
2537 status = -EFAULT;
2538 break;
2539 }
2540
2541 mutex_lock(&device->mutex);
2542
2543 if (enable) {
2544 device->pwrctrl.ctrl_flags = 0;
2545
2546 if (!kgsl_active_count_get(device)) {
2547 adreno_fault_detect_start(adreno_dev);
2548 kgsl_active_count_put(device);
2549 }
2550
2551 kgsl_pwrscale_enable(device);
2552 } else {
2553 kgsl_pwrctrl_change_state(device,
2554 KGSL_STATE_ACTIVE);
2555 device->pwrctrl.ctrl_flags = KGSL_PWR_ON;
2556 adreno_fault_detect_stop(adreno_dev);
2557 kgsl_pwrscale_disable(device, true);
2558 }
2559
2560 mutex_unlock(&device->mutex);
2561 status = 0;
2562 }
2563 break;
2564 case KGSL_PROP_PWR_CONSTRAINT: {
2565 struct kgsl_device_constraint constraint;
2566 struct kgsl_context *context;
2567
2568 if (sizebytes != sizeof(constraint))
2569 break;
2570
2571 if (copy_from_user(&constraint, value,
2572 sizeof(constraint))) {
2573 status = -EFAULT;
2574 break;
2575 }
2576
2577 context = kgsl_context_get_owner(dev_priv,
2578 constraint.context_id);
2579
2580 if (context == NULL)
2581 break;
2582
2583 status = adreno_set_constraint(device, context,
2584 &constraint);
2585
2586 kgsl_context_put(context);
2587 }
2588 break;
2589 default:
2590 break;
2591 }
2592
2593 return status;
2594}
2595
2596/*
2597 * adreno_irq_pending() - Checks if interrupt is generated by h/w
2598 * @adreno_dev: Pointer to device whose interrupts are checked
2599 *
2600 * Returns true if interrupts are pending from device else 0.
2601 */
2602inline unsigned int adreno_irq_pending(struct adreno_device *adreno_dev)
2603{
2604 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
2605 unsigned int status;
2606
2607 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_INT_0_STATUS, &status);
2608
Deepak Kumar273c5712017-01-03 21:49:03 +05302609 /*
2610 * IRQ handler clears the RBBM INT0 status register immediately
2611 * entering the ISR before actually serving the interrupt because
2612 * of this we can't rely only on RBBM INT0 status only.
2613 * Use pending_irq_refcnt along with RBBM INT0 to correctly
2614 * determine whether any IRQ is pending or not.
2615 */
2616 if ((status & gpudev->irq->mask) ||
2617 atomic_read(&adreno_dev->pending_irq_refcnt))
2618 return 1;
2619 else
2620 return 0;
Shrenuj Bansala419c792016-10-20 14:05:11 -07002621}
2622
2623
2624/**
2625 * adreno_hw_isidle() - Check if the GPU core is idle
2626 * @adreno_dev: Pointer to the Adreno device structure for the GPU
2627 *
2628 * Return true if the RBBM status register for the GPU type indicates that the
2629 * hardware is idle
2630 */
2631bool adreno_hw_isidle(struct adreno_device *adreno_dev)
2632{
2633 const struct adreno_gpu_core *gpucore = adreno_dev->gpucore;
2634 unsigned int reg_rbbm_status;
Oleg Perelet62d5cec2017-03-27 16:14:52 -07002635 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
2636
2637 /* if hw driver implements idle check - use it */
2638 if (gpudev->hw_isidle)
2639 return gpudev->hw_isidle(adreno_dev);
Shrenuj Bansala419c792016-10-20 14:05:11 -07002640
2641 if (adreno_is_a540(adreno_dev))
2642 /**
2643 * Due to CRC idle throttling GPU
2644 * idle hysteresys can take up to
2645 * 3usec for expire - account for it
2646 */
2647 udelay(5);
2648
2649 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS,
2650 &reg_rbbm_status);
2651
2652 if (reg_rbbm_status & gpucore->busy_mask)
2653 return false;
2654
2655 /* Don't consider ourselves idle if there is an IRQ pending */
2656 if (adreno_irq_pending(adreno_dev))
2657 return false;
2658
2659 return true;
2660}
2661
2662/**
2663 * adreno_soft_reset() - Do a soft reset of the GPU hardware
2664 * @device: KGSL device to soft reset
2665 *
2666 * "soft reset" the GPU hardware - this is a fast path GPU reset
2667 * The GPU hardware is reset but we never pull power so we can skip
2668 * a lot of the standard adreno_stop/adreno_start sequence
2669 */
Shrenuj Bansald0fe7462017-05-08 16:11:19 -07002670int adreno_soft_reset(struct kgsl_device *device)
Shrenuj Bansala419c792016-10-20 14:05:11 -07002671{
2672 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2673 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
2674 int ret;
2675
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -07002676 if (gpudev->oob_set) {
Kyle Piefer42de1402017-09-15 11:28:47 -07002677 ret = gpudev->oob_set(adreno_dev, OOB_GPU_SET_MASK,
2678 OOB_GPU_CHECK_MASK,
2679 OOB_GPU_CLEAR_MASK);
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -07002680 if (ret)
2681 return ret;
2682 }
2683
Shrenuj Bansala419c792016-10-20 14:05:11 -07002684 kgsl_pwrctrl_change_state(device, KGSL_STATE_AWARE);
2685 adreno_set_active_ctxs_null(adreno_dev);
2686
2687 adreno_irqctrl(adreno_dev, 0);
2688
2689 adreno_clear_gpu_fault(adreno_dev);
2690 /* since device is oficially off now clear start bit */
2691 clear_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);
2692
2693 /* save physical performance counter values before GPU soft reset */
2694 adreno_perfcounter_save(adreno_dev);
2695
2696 /* Reset the GPU */
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -07002697 if (gpudev->soft_reset)
2698 ret = gpudev->soft_reset(adreno_dev);
2699 else
2700 ret = _soft_reset(adreno_dev);
2701 if (ret) {
2702 if (gpudev->oob_clear)
Kyle Piefer42de1402017-09-15 11:28:47 -07002703 gpudev->oob_clear(adreno_dev, OOB_GPU_CLEAR_MASK);
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -07002704 return ret;
2705 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07002706
Abhilash Kumare0118252017-06-15 12:25:24 +05302707 /* Clear the busy_data stats - we're starting over from scratch */
2708 adreno_dev->busy_data.gpu_busy = 0;
Deepak Kumar84b9e032017-11-08 13:08:50 +05302709 adreno_dev->busy_data.bif_ram_cycles = 0;
2710 adreno_dev->busy_data.bif_ram_cycles_read_ch1 = 0;
2711 adreno_dev->busy_data.bif_ram_cycles_write_ch0 = 0;
2712 adreno_dev->busy_data.bif_ram_cycles_write_ch1 = 0;
2713 adreno_dev->busy_data.bif_starved_ram = 0;
2714 adreno_dev->busy_data.bif_starved_ram_ch1 = 0;
Abhilash Kumare0118252017-06-15 12:25:24 +05302715
Shrenuj Bansala419c792016-10-20 14:05:11 -07002716 /* Set the page table back to the default page table */
2717 adreno_ringbuffer_set_global(adreno_dev, 0);
2718 kgsl_mmu_set_pt(&device->mmu, device->mmu.defaultpagetable);
2719
2720 _set_secvid(device);
2721
2722 /* Enable 64 bit gpu addr if feature is set */
2723 if (gpudev->enable_64bit &&
2724 adreno_support_64bit(adreno_dev))
2725 gpudev->enable_64bit(adreno_dev);
2726
2727
2728 /* Reinitialize the GPU */
2729 gpudev->start(adreno_dev);
2730
2731 /* Re-initialize the coresight registers if applicable */
2732 adreno_coresight_start(adreno_dev);
2733
2734 /* Enable IRQ */
2735 adreno_irqctrl(adreno_dev, 1);
2736
2737 /* stop all ringbuffers to cancel RB events */
2738 adreno_ringbuffer_stop(adreno_dev);
2739 /*
2740 * If we have offsets for the jump tables we can try to do a warm start,
2741 * otherwise do a full ringbuffer restart
2742 */
2743
2744 if (ADRENO_FEATURE(adreno_dev, ADRENO_WARM_START))
2745 ret = adreno_ringbuffer_start(adreno_dev, ADRENO_START_WARM);
2746 else
2747 ret = adreno_ringbuffer_start(adreno_dev, ADRENO_START_COLD);
2748 if (ret == 0) {
2749 device->reset_counter++;
2750 set_bit(ADRENO_DEVICE_STARTED, &adreno_dev->priv);
2751 }
2752
2753 /* Restore physical performance counter values after soft reset */
2754 adreno_perfcounter_restore(adreno_dev);
2755
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -07002756 if (gpudev->oob_clear)
Kyle Piefer42de1402017-09-15 11:28:47 -07002757 gpudev->oob_clear(adreno_dev, OOB_GPU_CLEAR_MASK);
Shrenuj Bansal49d0e9f2017-05-08 16:10:24 -07002758
Shrenuj Bansala419c792016-10-20 14:05:11 -07002759 return ret;
2760}
2761
2762/*
2763 * adreno_isidle() - return true if the GPU hardware is idle
2764 * @device: Pointer to the KGSL device structure for the GPU
2765 *
2766 * Return true if the GPU hardware is idle and there are no commands pending in
2767 * the ringbuffer
2768 */
2769bool adreno_isidle(struct kgsl_device *device)
2770{
2771 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2772 struct adreno_ringbuffer *rb;
2773 int i;
2774
2775 if (!kgsl_state_is_awake(device))
2776 return true;
2777
2778 /*
2779 * wptr is updated when we add commands to ringbuffer, add a barrier
2780 * to make sure updated wptr is compared to rptr
2781 */
2782 smp_mb();
2783
2784 /*
2785 * ringbuffer is truly idle when all ringbuffers read and write
2786 * pointers are equal
2787 */
2788
2789 FOR_EACH_RINGBUFFER(adreno_dev, rb, i) {
2790 if (!adreno_rb_empty(rb))
2791 return false;
2792 }
2793
2794 return adreno_hw_isidle(adreno_dev);
2795}
2796
Carter Cooper8567af02017-03-15 14:22:03 -06002797/* Print some key registers if a spin-for-idle times out */
2798void adreno_spin_idle_debug(struct adreno_device *adreno_dev,
2799 const char *str)
2800{
2801 struct kgsl_device *device = &adreno_dev->dev;
2802 unsigned int rptr, wptr;
2803 unsigned int status, status3, intstatus;
2804 unsigned int hwfault;
2805
2806 dev_err(device->dev, str);
2807
2808 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_RPTR, &rptr);
2809 adreno_readreg(adreno_dev, ADRENO_REG_CP_RB_WPTR, &wptr);
2810
2811 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS, &status);
2812 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_STATUS3, &status3);
2813 adreno_readreg(adreno_dev, ADRENO_REG_RBBM_INT_0_STATUS, &intstatus);
2814 adreno_readreg(adreno_dev, ADRENO_REG_CP_HW_FAULT, &hwfault);
2815
2816 dev_err(device->dev,
2817 "rb=%d pos=%X/%X rbbm_status=%8.8X/%8.8X int_0_status=%8.8X\n",
2818 adreno_dev->cur_rb->id, rptr, wptr, status, status3, intstatus);
2819
2820 dev_err(device->dev, " hwfault=%8.8X\n", hwfault);
2821
Lynus Vaz43695aa2017-09-01 21:55:23 +05302822 kgsl_device_snapshot(device, NULL, adreno_gmu_gpu_fault(adreno_dev));
Carter Cooper8567af02017-03-15 14:22:03 -06002823}
2824
Shrenuj Bansala419c792016-10-20 14:05:11 -07002825/**
2826 * adreno_spin_idle() - Spin wait for the GPU to idle
2827 * @adreno_dev: Pointer to an adreno device
2828 * @timeout: milliseconds to wait before returning error
2829 *
2830 * Spin the CPU waiting for the RBBM status to return idle
2831 */
2832int adreno_spin_idle(struct adreno_device *adreno_dev, unsigned int timeout)
2833{
2834 unsigned long wait = jiffies + msecs_to_jiffies(timeout);
2835
2836 do {
2837 /*
2838 * If we fault, stop waiting and return an error. The dispatcher
2839 * will clean up the fault from the work queue, but we need to
2840 * make sure we don't block it by waiting for an idle that
2841 * will never come.
2842 */
2843
2844 if (adreno_gpu_fault(adreno_dev) != 0)
2845 return -EDEADLK;
2846
2847 if (adreno_isidle(KGSL_DEVICE(adreno_dev)))
2848 return 0;
2849
2850 } while (time_before(jiffies, wait));
2851
2852 /*
2853 * Under rare conditions, preemption can cause the while loop to exit
2854 * without checking if the gpu is idle. check one last time before we
2855 * return failure.
2856 */
2857 if (adreno_gpu_fault(adreno_dev) != 0)
2858 return -EDEADLK;
2859
2860 if (adreno_isidle(KGSL_DEVICE(adreno_dev)))
2861 return 0;
2862
2863 return -ETIMEDOUT;
2864}
2865
2866/**
2867 * adreno_idle() - wait for the GPU hardware to go idle
2868 * @device: Pointer to the KGSL device structure for the GPU
2869 *
2870 * Wait up to ADRENO_IDLE_TIMEOUT milliseconds for the GPU hardware to go quiet.
2871 * Caller must hold the device mutex, and must not hold the dispatcher mutex.
2872 */
2873
2874int adreno_idle(struct kgsl_device *device)
2875{
2876 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
2877 int ret;
2878
2879 /*
2880 * Make sure the device mutex is held so the dispatcher can't send any
2881 * more commands to the hardware
2882 */
2883
2884 if (WARN_ON(!mutex_is_locked(&device->mutex)))
2885 return -EDEADLK;
2886
2887 /* Check if we are already idle before idling dispatcher */
2888 if (adreno_isidle(device))
2889 return 0;
2890 /*
2891 * Wait for dispatcher to finish completing commands
2892 * already submitted
2893 */
2894 ret = adreno_dispatcher_idle(adreno_dev);
2895 if (ret)
2896 return ret;
2897
2898 return adreno_spin_idle(adreno_dev, ADRENO_IDLE_TIMEOUT);
2899}
2900
2901/**
2902 * adreno_drain() - Drain the dispatch queue
2903 * @device: Pointer to the KGSL device structure for the GPU
2904 *
2905 * Drain the dispatcher of existing drawobjs. This halts
2906 * additional commands from being issued until the gate is completed.
2907 */
2908static int adreno_drain(struct kgsl_device *device)
2909{
2910 reinit_completion(&device->halt_gate);
2911
2912 return 0;
2913}
2914
2915/* Caller must hold the device mutex. */
2916static int adreno_suspend_context(struct kgsl_device *device)
2917{
2918 /* process any profiling results that are available */
2919 adreno_profile_process_results(ADRENO_DEVICE(device));
2920
2921 /* Wait for the device to go idle */
2922 return adreno_idle(device);
2923}
2924
2925/**
2926 * adreno_read - General read function to read adreno device memory
2927 * @device - Pointer to the GPU device struct (for adreno device)
2928 * @base - Base address (kernel virtual) where the device memory is mapped
2929 * @offsetwords - Offset in words from the base address, of the memory that
2930 * is to be read
2931 * @value - Value read from the device memory
2932 * @mem_len - Length of the device memory mapped to the kernel
2933 */
2934static void adreno_read(struct kgsl_device *device, void __iomem *base,
2935 unsigned int offsetwords, unsigned int *value,
2936 unsigned int mem_len)
2937{
2938
2939 void __iomem *reg;
2940
2941 /* Make sure we're not reading from invalid memory */
2942 if (WARN(offsetwords * sizeof(uint32_t) >= mem_len,
2943 "Out of bounds register read: 0x%x/0x%x\n",
2944 offsetwords, mem_len >> 2))
2945 return;
2946
2947 reg = (base + (offsetwords << 2));
2948
2949 if (!in_interrupt())
2950 kgsl_pre_hwaccess(device);
2951
2952 *value = __raw_readl(reg);
2953 /*
2954 * ensure this read finishes before the next one.
2955 * i.e. act like normal readl()
2956 */
2957 rmb();
2958}
2959
2960/**
2961 * adreno_regread - Used to read adreno device registers
2962 * @offsetwords - Word (4 Bytes) offset to the register to be read
2963 * @value - Value read from device register
2964 */
2965static void adreno_regread(struct kgsl_device *device, unsigned int offsetwords,
2966 unsigned int *value)
2967{
2968 adreno_read(device, device->reg_virt, offsetwords, value,
2969 device->reg_len);
2970}
2971
2972/**
2973 * adreno_shadermem_regread - Used to read GPU (adreno) shader memory
2974 * @device - GPU device whose shader memory is to be read
2975 * @offsetwords - Offset in words, of the shader memory address to be read
2976 * @value - Pointer to where the read shader mem value is to be stored
2977 */
2978void adreno_shadermem_regread(struct kgsl_device *device,
2979 unsigned int offsetwords, unsigned int *value)
2980{
2981 adreno_read(device, device->shader_mem_virt, offsetwords, value,
2982 device->shader_mem_len);
2983}
2984
2985static void adreno_regwrite(struct kgsl_device *device,
2986 unsigned int offsetwords,
2987 unsigned int value)
2988{
2989 void __iomem *reg;
2990
2991 /* Make sure we're not writing to an invalid register */
2992 if (WARN(offsetwords * sizeof(uint32_t) >= device->reg_len,
2993 "Out of bounds register write: 0x%x/0x%x\n",
2994 offsetwords, device->reg_len >> 2))
2995 return;
2996
2997 if (!in_interrupt())
2998 kgsl_pre_hwaccess(device);
2999
3000 trace_kgsl_regwrite(device, offsetwords, value);
3001
3002 reg = (device->reg_virt + (offsetwords << 2));
3003
3004 /*
3005 * ensure previous writes post before this one,
3006 * i.e. act like normal writel()
3007 */
3008 wmb();
3009 __raw_writel(value, reg);
3010}
3011
Kyle Pieferb1027b02017-02-10 13:58:58 -08003012static void adreno_gmu_regwrite(struct kgsl_device *device,
3013 unsigned int offsetwords,
3014 unsigned int value)
3015{
3016 void __iomem *reg;
3017 struct gmu_device *gmu = &device->gmu;
3018
Kyle Pieferb1027b02017-02-10 13:58:58 -08003019 trace_kgsl_regwrite(device, offsetwords, value);
3020
Kyle Pieferda6ef632017-06-29 13:18:51 -07003021 offsetwords -= gmu->gmu2gpu_offset;
Kyle Pieferb1027b02017-02-10 13:58:58 -08003022 reg = gmu->reg_virt + (offsetwords << 2);
3023
3024 /*
3025 * ensure previous writes post before this one,
3026 * i.e. act like normal writel()
3027 */
3028 wmb();
3029 __raw_writel(value, reg);
3030}
3031
3032static void adreno_gmu_regread(struct kgsl_device *device,
3033 unsigned int offsetwords,
3034 unsigned int *value)
3035{
3036 void __iomem *reg;
3037 struct gmu_device *gmu = &device->gmu;
3038
3039 offsetwords -= gmu->gmu2gpu_offset;
3040
3041 reg = gmu->reg_virt + (offsetwords << 2);
3042
3043 *value = __raw_readl(reg);
3044
3045 /*
3046 * ensure this read finishes before the next one.
3047 * i.e. act like normal readl()
3048 */
3049 rmb();
3050}
3051
Lynus Vaz9ed8cf92017-09-21 21:55:34 +05303052bool adreno_is_cx_dbgc_register(struct kgsl_device *device,
3053 unsigned int offsetwords)
3054{
3055 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3056
3057 return adreno_dev->cx_dbgc_virt &&
3058 (offsetwords >= (adreno_dev->cx_dbgc_base >> 2)) &&
3059 (offsetwords < (adreno_dev->cx_dbgc_base +
3060 adreno_dev->cx_dbgc_len) >> 2);
3061}
3062
3063void adreno_cx_dbgc_regread(struct kgsl_device *device,
3064 unsigned int offsetwords, unsigned int *value)
3065{
3066 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3067 unsigned int cx_dbgc_offset;
3068
3069 if (!adreno_is_cx_dbgc_register(device, offsetwords))
3070 return;
3071
3072 cx_dbgc_offset = (offsetwords << 2) - adreno_dev->cx_dbgc_base;
3073 *value = __raw_readl(adreno_dev->cx_dbgc_virt + cx_dbgc_offset);
3074
3075 /*
3076 * ensure this read finishes before the next one.
3077 * i.e. act like normal readl()
3078 */
3079 rmb();
3080}
3081
3082void adreno_cx_dbgc_regwrite(struct kgsl_device *device,
3083 unsigned int offsetwords, unsigned int value)
3084{
3085 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3086 unsigned int cx_dbgc_offset;
3087
3088 if (!adreno_is_cx_dbgc_register(device, offsetwords))
3089 return;
3090
3091 cx_dbgc_offset = (offsetwords << 2) - adreno_dev->cx_dbgc_base;
3092 trace_kgsl_regwrite(device, offsetwords, value);
3093
3094 /*
3095 * ensure previous writes post before this one,
3096 * i.e. act like normal writel()
3097 */
3098 wmb();
3099 __raw_writel(value, adreno_dev->cx_dbgc_virt + cx_dbgc_offset);
3100}
3101
Shrenuj Bansala419c792016-10-20 14:05:11 -07003102/**
3103 * adreno_waittimestamp - sleep while waiting for the specified timestamp
3104 * @device - pointer to a KGSL device structure
3105 * @context - pointer to the active kgsl context
3106 * @timestamp - GPU timestamp to wait for
3107 * @msecs - amount of time to wait (in milliseconds)
3108 *
3109 * Wait up to 'msecs' milliseconds for the specified timestamp to expire.
3110 */
3111static int adreno_waittimestamp(struct kgsl_device *device,
3112 struct kgsl_context *context,
3113 unsigned int timestamp,
3114 unsigned int msecs)
3115{
3116 int ret;
3117
3118 if (context == NULL) {
3119 /* If they are doing then complain once */
3120 dev_WARN_ONCE(device->dev, 1,
3121 "IOCTL_KGSL_DEVICE_WAITTIMESTAMP is deprecated\n");
3122 return -ENOTTY;
3123 }
3124
3125 /* Return -ENOENT if the context has been detached */
3126 if (kgsl_context_detached(context))
3127 return -ENOENT;
3128
3129 ret = adreno_drawctxt_wait(ADRENO_DEVICE(device), context,
3130 timestamp, msecs);
3131
3132 /* If the context got invalidated then return a specific error */
3133 if (kgsl_context_invalid(context))
3134 ret = -EDEADLK;
3135
3136 /*
3137 * Return -EPROTO if the device has faulted since the last time we
3138 * checked. Userspace uses this as a marker for performing post
3139 * fault activities
3140 */
3141
3142 if (!ret && test_and_clear_bit(ADRENO_CONTEXT_FAULT, &context->priv))
3143 ret = -EPROTO;
3144
3145 return ret;
3146}
3147
3148/**
3149 * __adreno_readtimestamp() - Reads the timestamp from memstore memory
3150 * @adreno_dev: Pointer to an adreno device
3151 * @index: Index into the memstore memory
3152 * @type: Type of timestamp to read
3153 * @timestamp: The out parameter where the timestamp is read
3154 */
3155static int __adreno_readtimestamp(struct adreno_device *adreno_dev, int index,
3156 int type, unsigned int *timestamp)
3157{
3158 struct kgsl_device *device = KGSL_DEVICE(adreno_dev);
3159 int status = 0;
3160
3161 switch (type) {
3162 case KGSL_TIMESTAMP_CONSUMED:
3163 kgsl_sharedmem_readl(&device->memstore, timestamp,
3164 KGSL_MEMSTORE_OFFSET(index, soptimestamp));
3165 break;
3166 case KGSL_TIMESTAMP_RETIRED:
3167 kgsl_sharedmem_readl(&device->memstore, timestamp,
3168 KGSL_MEMSTORE_OFFSET(index, eoptimestamp));
3169 break;
3170 default:
3171 status = -EINVAL;
3172 *timestamp = 0;
3173 break;
3174 }
3175 return status;
3176}
3177
3178/**
3179 * adreno_rb_readtimestamp(): Return the value of given type of timestamp
3180 * for a RB
3181 * @adreno_dev: adreno device whose timestamp values are being queried
3182 * @priv: The object being queried for a timestamp (expected to be a rb pointer)
3183 * @type: The type of timestamp (one of 3) to be read
3184 * @timestamp: Pointer to where the read timestamp is to be written to
3185 *
3186 * CONSUMED and RETIRED type timestamps are sorted by id and are constantly
3187 * updated by the GPU through shared memstore memory. QUEUED type timestamps
3188 * are read directly from context struct.
3189
3190 * The function returns 0 on success and timestamp value at the *timestamp
3191 * address and returns -EINVAL on any read error/invalid type and timestamp = 0.
3192 */
3193int adreno_rb_readtimestamp(struct adreno_device *adreno_dev,
3194 void *priv, enum kgsl_timestamp_type type,
3195 unsigned int *timestamp)
3196{
3197 int status = 0;
3198 struct adreno_ringbuffer *rb = priv;
3199
3200 if (type == KGSL_TIMESTAMP_QUEUED)
3201 *timestamp = rb->timestamp;
3202 else
3203 status = __adreno_readtimestamp(adreno_dev,
3204 rb->id + KGSL_MEMSTORE_MAX,
3205 type, timestamp);
3206
3207 return status;
3208}
3209
3210/**
3211 * adreno_readtimestamp(): Return the value of given type of timestamp
3212 * @device: GPU device whose timestamp values are being queried
3213 * @priv: The object being queried for a timestamp (expected to be a context)
3214 * @type: The type of timestamp (one of 3) to be read
3215 * @timestamp: Pointer to where the read timestamp is to be written to
3216 *
3217 * CONSUMED and RETIRED type timestamps are sorted by id and are constantly
3218 * updated by the GPU through shared memstore memory. QUEUED type timestamps
3219 * are read directly from context struct.
3220
3221 * The function returns 0 on success and timestamp value at the *timestamp
3222 * address and returns -EINVAL on any read error/invalid type and timestamp = 0.
3223 */
3224static int adreno_readtimestamp(struct kgsl_device *device,
3225 void *priv, enum kgsl_timestamp_type type,
3226 unsigned int *timestamp)
3227{
3228 int status = 0;
3229 struct kgsl_context *context = priv;
3230
3231 if (type == KGSL_TIMESTAMP_QUEUED) {
3232 struct adreno_context *ctxt = ADRENO_CONTEXT(context);
3233
3234 *timestamp = ctxt->timestamp;
3235 } else
3236 status = __adreno_readtimestamp(ADRENO_DEVICE(device),
3237 context->id, type, timestamp);
3238
3239 return status;
3240}
3241
3242static inline s64 adreno_ticks_to_us(u32 ticks, u32 freq)
3243{
3244 freq /= 1000000;
3245 return ticks / freq;
3246}
3247
3248/**
3249 * adreno_power_stats() - Reads the counters needed for freq decisions
3250 * @device: Pointer to device whose counters are read
3251 * @stats: Pointer to stats set that needs updating
3252 * Power: The caller is expected to be in a clock enabled state as this
3253 * function does reg reads
3254 */
3255static void adreno_power_stats(struct kgsl_device *device,
3256 struct kgsl_power_stats *stats)
3257{
3258 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3259 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
3260 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
3261 struct adreno_busy_data *busy = &adreno_dev->busy_data;
3262 uint64_t adj = 0;
3263
3264 memset(stats, 0, sizeof(*stats));
3265
3266 /* Get the busy cycles counted since the counter was last reset */
3267 if (adreno_dev->perfctr_pwr_lo != 0) {
3268 uint64_t gpu_busy;
3269
3270 gpu_busy = counter_delta(device, adreno_dev->perfctr_pwr_lo,
3271 &busy->gpu_busy);
3272
3273 if (gpudev->read_throttling_counters) {
3274 adj = gpudev->read_throttling_counters(adreno_dev);
3275 gpu_busy += adj;
3276 }
3277
Deepak Kumar2c8ea992017-09-18 19:59:17 +05303278 if (adreno_is_a6xx(adreno_dev)) {
George Shen07550732017-06-01 11:54:16 -07003279 /* clock sourced from XO */
Maria Yu2a69efa2017-09-26 16:29:58 +08003280 stats->busy_time = gpu_busy * 10;
3281 do_div(stats->busy_time, 192);
George Shen07550732017-06-01 11:54:16 -07003282 } else {
3283 /* clock sourced from GFX3D */
3284 stats->busy_time = adreno_ticks_to_us(gpu_busy,
3285 kgsl_pwrctrl_active_freq(pwr));
3286 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003287 }
3288
3289 if (device->pwrctrl.bus_control) {
3290 uint64_t ram_cycles = 0, starved_ram = 0;
3291
3292 if (adreno_dev->ram_cycles_lo != 0)
3293 ram_cycles = counter_delta(device,
3294 adreno_dev->ram_cycles_lo,
Deepak Kumar84b9e032017-11-08 13:08:50 +05303295 &busy->bif_ram_cycles);
3296
3297 if (adreno_has_gbif(adreno_dev)) {
3298 if (adreno_dev->ram_cycles_lo_ch1_read != 0)
3299 ram_cycles += counter_delta(device,
3300 adreno_dev->ram_cycles_lo_ch1_read,
3301 &busy->bif_ram_cycles_read_ch1);
3302
3303 if (adreno_dev->ram_cycles_lo_ch0_write != 0)
3304 ram_cycles += counter_delta(device,
3305 adreno_dev->ram_cycles_lo_ch0_write,
3306 &busy->bif_ram_cycles_write_ch0);
3307
3308 if (adreno_dev->ram_cycles_lo_ch1_write != 0)
3309 ram_cycles += counter_delta(device,
3310 adreno_dev->ram_cycles_lo_ch1_write,
3311 &busy->bif_ram_cycles_write_ch1);
3312 }
Shrenuj Bansala419c792016-10-20 14:05:11 -07003313
3314 if (adreno_dev->starved_ram_lo != 0)
3315 starved_ram = counter_delta(device,
3316 adreno_dev->starved_ram_lo,
Deepak Kumar84b9e032017-11-08 13:08:50 +05303317 &busy->bif_starved_ram);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003318
Deepak Kumarc52781f2017-11-06 16:10:17 +05303319 if (adreno_has_gbif(adreno_dev)) {
3320 if (adreno_dev->starved_ram_lo_ch1 != 0)
3321 starved_ram += counter_delta(device,
3322 adreno_dev->starved_ram_lo_ch1,
Deepak Kumar84b9e032017-11-08 13:08:50 +05303323 &busy->bif_starved_ram_ch1);
Deepak Kumarc52781f2017-11-06 16:10:17 +05303324 }
3325
Shrenuj Bansala419c792016-10-20 14:05:11 -07003326 stats->ram_time = ram_cycles;
3327 stats->ram_wait = starved_ram;
3328 }
3329 if (adreno_dev->lm_threshold_count &&
3330 gpudev->count_throttles)
3331 gpudev->count_throttles(adreno_dev, adj);
3332}
3333
3334static unsigned int adreno_gpuid(struct kgsl_device *device,
3335 unsigned int *chipid)
3336{
3337 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3338
3339 /*
3340 * Some applications need to know the chip ID too, so pass
3341 * that as a parameter
3342 */
3343
3344 if (chipid != NULL)
3345 *chipid = adreno_dev->chipid;
3346
3347 /*
3348 * Standard KGSL gpuid format:
3349 * top word is 0x0002 for 2D or 0x0003 for 3D
3350 * Bottom word is core specific identifer
3351 */
3352
3353 return (0x0003 << 16) | ADRENO_GPUREV(adreno_dev);
3354}
3355
3356static int adreno_regulator_enable(struct kgsl_device *device)
3357{
3358 int ret = 0;
3359 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3360 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
3361
3362 if (gpudev->regulator_enable &&
3363 !test_bit(ADRENO_DEVICE_GPU_REGULATOR_ENABLED,
3364 &adreno_dev->priv)) {
3365 ret = gpudev->regulator_enable(adreno_dev);
3366 if (!ret)
3367 set_bit(ADRENO_DEVICE_GPU_REGULATOR_ENABLED,
3368 &adreno_dev->priv);
3369 }
3370 return ret;
3371}
3372
3373static bool adreno_is_hw_collapsible(struct kgsl_device *device)
3374{
3375 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3376 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
3377
3378 /*
3379 * Skip power collapse for A304, if power ctrl flag is set to
3380 * non zero. As A304 soft_reset will not work, power collapse
3381 * needs to disable to avoid soft_reset.
3382 */
3383 if (adreno_is_a304(adreno_dev) &&
3384 device->pwrctrl.ctrl_flags)
3385 return false;
3386
3387 return adreno_isidle(device) && (gpudev->is_sptp_idle ?
3388 gpudev->is_sptp_idle(adreno_dev) : true);
3389}
3390
3391static void adreno_regulator_disable(struct kgsl_device *device)
3392{
3393 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3394 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
3395
3396 if (gpudev->regulator_disable &&
3397 test_bit(ADRENO_DEVICE_GPU_REGULATOR_ENABLED,
3398 &adreno_dev->priv)) {
3399 gpudev->regulator_disable(adreno_dev);
3400 clear_bit(ADRENO_DEVICE_GPU_REGULATOR_ENABLED,
3401 &adreno_dev->priv);
3402 }
3403}
3404
3405static void adreno_pwrlevel_change_settings(struct kgsl_device *device,
3406 unsigned int prelevel, unsigned int postlevel, bool post)
3407{
3408 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3409 struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev);
3410
3411 if (gpudev->pwrlevel_change_settings)
3412 gpudev->pwrlevel_change_settings(adreno_dev, prelevel,
3413 postlevel, post);
3414}
3415
3416static void adreno_clk_set_options(struct kgsl_device *device, const char *name,
Deepak Kumara309e0e2017-03-17 17:27:42 +05303417 struct clk *clk, bool on)
Shrenuj Bansala419c792016-10-20 14:05:11 -07003418{
3419 if (ADRENO_GPU_DEVICE(ADRENO_DEVICE(device))->clk_set_options)
3420 ADRENO_GPU_DEVICE(ADRENO_DEVICE(device))->clk_set_options(
Deepak Kumara309e0e2017-03-17 17:27:42 +05303421 ADRENO_DEVICE(device), name, clk, on);
Shrenuj Bansala419c792016-10-20 14:05:11 -07003422}
3423
3424static void adreno_iommu_sync(struct kgsl_device *device, bool sync)
3425{
3426 struct scm_desc desc = {0};
3427 int ret;
3428
3429 if (sync == true) {
3430 mutex_lock(&kgsl_mmu_sync);
3431 desc.args[0] = true;
3432 desc.arginfo = SCM_ARGS(1);
3433 ret = scm_call2_atomic(SCM_SIP_FNID(SCM_SVC_PWR, 0x8), &desc);
3434 if (ret)
3435 KGSL_DRV_ERR(device,
3436 "MMU sync with Hypervisor off %x\n", ret);
3437 } else {
3438 desc.args[0] = false;
3439 desc.arginfo = SCM_ARGS(1);
3440 scm_call2_atomic(SCM_SIP_FNID(SCM_SVC_PWR, 0x8), &desc);
3441 mutex_unlock(&kgsl_mmu_sync);
3442 }
3443}
3444
3445static void _regulator_disable(struct kgsl_regulator *regulator, bool poll)
3446{
3447 unsigned long wait_time = jiffies + msecs_to_jiffies(200);
3448
3449 if (IS_ERR_OR_NULL(regulator->reg))
3450 return;
3451
3452 regulator_disable(regulator->reg);
3453
3454 if (poll == false)
3455 return;
3456
3457 while (!time_after(jiffies, wait_time)) {
3458 if (!regulator_is_enabled(regulator->reg))
3459 return;
3460 cpu_relax();
3461 }
3462
3463 KGSL_CORE_ERR("regulator '%s' still on after 200ms\n", regulator->name);
3464}
3465
3466static void adreno_regulator_disable_poll(struct kgsl_device *device)
3467{
3468 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3469 struct kgsl_pwrctrl *pwr = &device->pwrctrl;
3470 int i;
3471
3472 /* Fast path - hopefully we don't need this quirk */
3473 if (!ADRENO_QUIRK(adreno_dev, ADRENO_QUIRK_IOMMU_SYNC)) {
3474 for (i = KGSL_MAX_REGULATORS - 1; i >= 0; i--)
3475 _regulator_disable(&pwr->regulators[i], false);
3476 return;
3477 }
3478
3479 adreno_iommu_sync(device, true);
3480
3481 for (i = 0; i < KGSL_MAX_REGULATORS; i++)
3482 _regulator_disable(&pwr->regulators[i], true);
3483
3484 adreno_iommu_sync(device, false);
3485}
3486
3487static void adreno_gpu_model(struct kgsl_device *device, char *str,
3488 size_t bufsz)
3489{
3490 struct adreno_device *adreno_dev = ADRENO_DEVICE(device);
3491
3492 snprintf(str, bufsz, "Adreno%d%d%dv%d",
3493 ADRENO_CHIPID_CORE(adreno_dev->chipid),
3494 ADRENO_CHIPID_MAJOR(adreno_dev->chipid),
3495 ADRENO_CHIPID_MINOR(adreno_dev->chipid),
3496 ADRENO_CHIPID_PATCH(adreno_dev->chipid) + 1);
3497}
3498
3499static const struct kgsl_functable adreno_functable = {
3500 /* Mandatory functions */
3501 .regread = adreno_regread,
3502 .regwrite = adreno_regwrite,
Kyle Pieferb1027b02017-02-10 13:58:58 -08003503 .gmu_regread = adreno_gmu_regread,
3504 .gmu_regwrite = adreno_gmu_regwrite,
Shrenuj Bansala419c792016-10-20 14:05:11 -07003505 .idle = adreno_idle,
3506 .isidle = adreno_isidle,
3507 .suspend_context = adreno_suspend_context,
3508 .init = adreno_init,
3509 .start = adreno_start,
3510 .stop = adreno_stop,
3511 .getproperty = adreno_getproperty,
3512 .getproperty_compat = adreno_getproperty_compat,
3513 .waittimestamp = adreno_waittimestamp,
3514 .readtimestamp = adreno_readtimestamp,
3515 .queue_cmds = adreno_dispatcher_queue_cmds,
3516 .ioctl = adreno_ioctl,
3517 .compat_ioctl = adreno_compat_ioctl,
3518 .power_stats = adreno_power_stats,
3519 .gpuid = adreno_gpuid,
3520 .snapshot = adreno_snapshot,
3521 .irq_handler = adreno_irq_handler,
3522 .drain = adreno_drain,
3523 /* Optional functions */
Carter Cooperb88b7082017-09-14 09:03:26 -06003524 .snapshot_gmu = adreno_snapshot_gmu,
Shrenuj Bansala419c792016-10-20 14:05:11 -07003525 .drawctxt_create = adreno_drawctxt_create,
3526 .drawctxt_detach = adreno_drawctxt_detach,
3527 .drawctxt_destroy = adreno_drawctxt_destroy,
3528 .drawctxt_dump = adreno_drawctxt_dump,
3529 .setproperty = adreno_setproperty,
3530 .setproperty_compat = adreno_setproperty_compat,
3531 .drawctxt_sched = adreno_drawctxt_sched,
3532 .resume = adreno_dispatcher_start,
3533 .regulator_enable = adreno_regulator_enable,
3534 .is_hw_collapsible = adreno_is_hw_collapsible,
3535 .regulator_disable = adreno_regulator_disable,
3536 .pwrlevel_change_settings = adreno_pwrlevel_change_settings,
3537 .regulator_disable_poll = adreno_regulator_disable_poll,
3538 .clk_set_options = adreno_clk_set_options,
3539 .gpu_model = adreno_gpu_model,
Hareesh Gundua2fe6ec2017-03-06 14:53:36 +05303540 .stop_fault_timer = adreno_dispatcher_stop_fault_timer,
Shrenuj Bansala419c792016-10-20 14:05:11 -07003541};
3542
3543static struct platform_driver adreno_platform_driver = {
3544 .probe = adreno_probe,
3545 .remove = adreno_remove,
3546 .suspend = kgsl_suspend_driver,
3547 .resume = kgsl_resume_driver,
3548 .id_table = adreno_id_table,
3549 .driver = {
3550 .owner = THIS_MODULE,
3551 .name = DEVICE_3D_NAME,
3552 .pm = &kgsl_pm_ops,
3553 .of_match_table = adreno_match_table,
3554 }
3555};
3556
3557static const struct of_device_id busmon_match_table[] = {
3558 { .compatible = "qcom,kgsl-busmon", .data = &device_3d0 },
3559 {}
3560};
3561
3562static int adreno_busmon_probe(struct platform_device *pdev)
3563{
3564 struct kgsl_device *device;
3565 const struct of_device_id *pdid =
3566 of_match_device(busmon_match_table, &pdev->dev);
3567
3568 if (pdid == NULL)
3569 return -ENXIO;
3570
3571 device = (struct kgsl_device *)pdid->data;
3572 device->busmondev = &pdev->dev;
3573 dev_set_drvdata(device->busmondev, device);
3574
3575 return 0;
3576}
3577
3578static struct platform_driver kgsl_bus_platform_driver = {
3579 .probe = adreno_busmon_probe,
3580 .driver = {
3581 .owner = THIS_MODULE,
3582 .name = "kgsl-busmon",
3583 .of_match_table = busmon_match_table,
3584 }
3585};
3586
3587static int __init kgsl_3d_init(void)
3588{
3589 int ret;
3590
3591 ret = platform_driver_register(&kgsl_bus_platform_driver);
3592 if (ret)
3593 return ret;
3594
3595 ret = platform_driver_register(&adreno_platform_driver);
3596 if (ret)
3597 platform_driver_unregister(&kgsl_bus_platform_driver);
3598
3599 return ret;
3600}
3601
3602static void __exit kgsl_3d_exit(void)
3603{
3604 platform_driver_unregister(&adreno_platform_driver);
3605 platform_driver_unregister(&kgsl_bus_platform_driver);
3606}
3607
3608module_init(kgsl_3d_init);
3609module_exit(kgsl_3d_exit);
3610
3611MODULE_DESCRIPTION("3D Graphics driver");
3612MODULE_LICENSE("GPL v2");
3613MODULE_ALIAS("platform:kgsl_3d");