Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2002,2007-2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/ioctl.h> |
| 15 | #include "kgsl_device.h" |
| 16 | #include "adreno.h" |
| 17 | #include "adreno_a5xx.h" |
| 18 | |
| 19 | long adreno_ioctl_perfcounter_get(struct kgsl_device_private *dev_priv, |
| 20 | unsigned int cmd, void *data) |
| 21 | { |
| 22 | struct kgsl_device *device = dev_priv->device; |
| 23 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
| 24 | struct kgsl_perfcounter_get *get = data; |
| 25 | int result; |
| 26 | |
| 27 | mutex_lock(&device->mutex); |
| 28 | |
| 29 | /* |
| 30 | * adreno_perfcounter_get() is called by kernel clients |
| 31 | * during start(), so it is not safe to take an |
| 32 | * active count inside that function. |
| 33 | */ |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 34 | |
Carter Cooper | 05f2a6b | 2017-03-20 11:43:11 -0600 | [diff] [blame] | 35 | result = adreno_perfcntr_active_oob_get(adreno_dev); |
| 36 | if (result) { |
| 37 | mutex_unlock(&device->mutex); |
| 38 | return (long)result; |
| 39 | } |
| 40 | |
| 41 | result = adreno_perfcounter_get(adreno_dev, |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 42 | get->groupid, get->countable, &get->offset, |
| 43 | &get->offset_hi, PERFCOUNTER_FLAG_NONE); |
Carter Cooper | 05f2a6b | 2017-03-20 11:43:11 -0600 | [diff] [blame] | 44 | |
| 45 | adreno_perfcntr_active_oob_put(adreno_dev); |
| 46 | |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 47 | mutex_unlock(&device->mutex); |
| 48 | |
| 49 | return (long) result; |
| 50 | } |
| 51 | |
| 52 | long adreno_ioctl_perfcounter_put(struct kgsl_device_private *dev_priv, |
| 53 | unsigned int cmd, void *data) |
| 54 | { |
| 55 | struct kgsl_device *device = dev_priv->device; |
| 56 | struct adreno_device *adreno_dev = ADRENO_DEVICE(device); |
| 57 | struct kgsl_perfcounter_put *put = data; |
| 58 | int result; |
| 59 | |
| 60 | mutex_lock(&device->mutex); |
| 61 | result = adreno_perfcounter_put(adreno_dev, put->groupid, |
| 62 | put->countable, PERFCOUNTER_FLAG_NONE); |
| 63 | mutex_unlock(&device->mutex); |
| 64 | |
| 65 | return (long) result; |
| 66 | } |
| 67 | |
| 68 | static long adreno_ioctl_perfcounter_query(struct kgsl_device_private *dev_priv, |
| 69 | unsigned int cmd, void *data) |
| 70 | { |
| 71 | struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_priv->device); |
| 72 | struct kgsl_perfcounter_query *query = data; |
| 73 | |
| 74 | return (long) adreno_perfcounter_query_group(adreno_dev, query->groupid, |
| 75 | query->countables, query->count, &query->max_counters); |
| 76 | } |
| 77 | |
| 78 | static long adreno_ioctl_perfcounter_read(struct kgsl_device_private *dev_priv, |
| 79 | unsigned int cmd, void *data) |
| 80 | { |
| 81 | struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_priv->device); |
| 82 | struct kgsl_perfcounter_read *read = data; |
| 83 | |
| 84 | return (long) adreno_perfcounter_read_group(adreno_dev, read->reads, |
| 85 | read->count); |
| 86 | } |
| 87 | |
| 88 | static long adreno_ioctl_preemption_counters_query( |
| 89 | struct kgsl_device_private *dev_priv, |
| 90 | unsigned int cmd, void *data) |
| 91 | { |
| 92 | struct adreno_device *adreno_dev = ADRENO_DEVICE(dev_priv->device); |
| 93 | struct adreno_gpudev *gpudev = ADRENO_GPU_DEVICE(adreno_dev); |
| 94 | struct kgsl_preemption_counters_query *read = data; |
| 95 | int size_level = A5XX_CP_CTXRECORD_PREEMPTION_COUNTER_SIZE; |
| 96 | int levels_to_copy; |
| 97 | |
| 98 | if (!adreno_is_a5xx(adreno_dev) || |
Harshdeep Dhatt | 38e57d7 | 2017-08-30 13:24:07 -0600 | [diff] [blame] | 99 | !adreno_is_preemption_execution_enabled(adreno_dev)) |
Shrenuj Bansal | a419c79 | 2016-10-20 14:05:11 -0700 | [diff] [blame] | 100 | return -EOPNOTSUPP; |
| 101 | |
| 102 | if (read->size_user < size_level) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | /* Calculate number of preemption counter levels to copy to userspace */ |
| 106 | levels_to_copy = (read->size_user / size_level); |
| 107 | if (levels_to_copy > gpudev->num_prio_levels) |
| 108 | levels_to_copy = gpudev->num_prio_levels; |
| 109 | |
| 110 | if (copy_to_user((void __user *) (uintptr_t) read->counters, |
| 111 | adreno_dev->preempt.counters.hostptr, |
| 112 | levels_to_copy * size_level)) |
| 113 | return -EFAULT; |
| 114 | |
| 115 | read->max_priority_level = levels_to_copy; |
| 116 | read->size_priority_level = size_level; |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | long adreno_ioctl_helper(struct kgsl_device_private *dev_priv, |
| 122 | unsigned int cmd, unsigned long arg, |
| 123 | const struct kgsl_ioctl *cmds, int len) |
| 124 | { |
| 125 | unsigned char data[128] = { 0 }; |
| 126 | long ret; |
| 127 | int i; |
| 128 | |
| 129 | static DEFINE_RATELIMIT_STATE(_rs, |
| 130 | DEFAULT_RATELIMIT_INTERVAL, |
| 131 | DEFAULT_RATELIMIT_BURST); |
| 132 | |
| 133 | for (i = 0; i < len; i++) { |
| 134 | if (_IOC_NR(cmd) == _IOC_NR(cmds[i].cmd)) |
| 135 | break; |
| 136 | } |
| 137 | |
| 138 | if (i == len) { |
| 139 | KGSL_DRV_INFO(dev_priv->device, |
| 140 | "invalid ioctl code 0x%08X\n", cmd); |
| 141 | return -ENOIOCTLCMD; |
| 142 | } |
| 143 | |
| 144 | if (WARN_ON(_IOC_SIZE(cmds[i].cmd) > sizeof(data))) { |
| 145 | if (__ratelimit(&_rs)) |
| 146 | WARN(1, "data too big for ioctl 0x%08X: %d/%ld\n", |
| 147 | cmd, _IOC_SIZE(cmds[i].cmd), sizeof(data)); |
| 148 | return -EINVAL; |
| 149 | } |
| 150 | |
| 151 | if (_IOC_SIZE(cmds[i].cmd)) { |
| 152 | ret = kgsl_ioctl_copy_in(cmds[i].cmd, cmd, arg, data); |
| 153 | |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | } else { |
| 157 | memset(data, 0, sizeof(data)); |
| 158 | } |
| 159 | |
| 160 | ret = cmds[i].func(dev_priv, cmd, data); |
| 161 | |
| 162 | if (ret == 0 && _IOC_SIZE(cmds[i].cmd)) |
| 163 | ret = kgsl_ioctl_copy_out(cmds[i].cmd, cmd, arg, data); |
| 164 | |
| 165 | return ret; |
| 166 | } |
| 167 | |
| 168 | static struct kgsl_ioctl adreno_ioctl_funcs[] = { |
| 169 | { IOCTL_KGSL_PERFCOUNTER_GET, adreno_ioctl_perfcounter_get }, |
| 170 | { IOCTL_KGSL_PERFCOUNTER_PUT, adreno_ioctl_perfcounter_put }, |
| 171 | { IOCTL_KGSL_PERFCOUNTER_QUERY, adreno_ioctl_perfcounter_query }, |
| 172 | { IOCTL_KGSL_PERFCOUNTER_READ, adreno_ioctl_perfcounter_read }, |
| 173 | { IOCTL_KGSL_PREEMPTIONCOUNTER_QUERY, |
| 174 | adreno_ioctl_preemption_counters_query }, |
| 175 | }; |
| 176 | |
| 177 | long adreno_ioctl(struct kgsl_device_private *dev_priv, |
| 178 | unsigned int cmd, unsigned long arg) |
| 179 | { |
| 180 | return adreno_ioctl_helper(dev_priv, cmd, arg, |
| 181 | adreno_ioctl_funcs, ARRAY_SIZE(adreno_ioctl_funcs)); |
| 182 | } |