The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 Etnaviv Project |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/component.h> |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 18 | #include <linux/dma-fence.h> |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 19 | #include <linux/moduleparam.h> |
| 20 | #include <linux/of_device.h> |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 21 | #include <linux/thermal.h> |
Lucas Stach | ea1f572 | 2017-01-16 16:09:51 +0100 | [diff] [blame] | 22 | |
| 23 | #include "etnaviv_cmdbuf.h" |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 24 | #include "etnaviv_dump.h" |
| 25 | #include "etnaviv_gpu.h" |
| 26 | #include "etnaviv_gem.h" |
| 27 | #include "etnaviv_mmu.h" |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 28 | #include "etnaviv_perfmon.h" |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 29 | #include "etnaviv_sched.h" |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 30 | #include "common.xml.h" |
| 31 | #include "state.xml.h" |
| 32 | #include "state_hi.xml.h" |
| 33 | #include "cmdstream.xml.h" |
| 34 | |
Lucas Stach | c09d7f7 | 2018-01-04 13:40:03 +0100 | [diff] [blame] | 35 | #ifndef PHYS_OFFSET |
| 36 | #define PHYS_OFFSET 0 |
| 37 | #endif |
| 38 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 39 | static const struct platform_device_id gpu_ids[] = { |
| 40 | { .name = "etnaviv-gpu,2d" }, |
| 41 | { }, |
| 42 | }; |
| 43 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 44 | /* |
| 45 | * Driver functions: |
| 46 | */ |
| 47 | |
| 48 | int etnaviv_gpu_get_param(struct etnaviv_gpu *gpu, u32 param, u64 *value) |
| 49 | { |
| 50 | switch (param) { |
| 51 | case ETNAVIV_PARAM_GPU_MODEL: |
| 52 | *value = gpu->identity.model; |
| 53 | break; |
| 54 | |
| 55 | case ETNAVIV_PARAM_GPU_REVISION: |
| 56 | *value = gpu->identity.revision; |
| 57 | break; |
| 58 | |
| 59 | case ETNAVIV_PARAM_GPU_FEATURES_0: |
| 60 | *value = gpu->identity.features; |
| 61 | break; |
| 62 | |
| 63 | case ETNAVIV_PARAM_GPU_FEATURES_1: |
| 64 | *value = gpu->identity.minor_features0; |
| 65 | break; |
| 66 | |
| 67 | case ETNAVIV_PARAM_GPU_FEATURES_2: |
| 68 | *value = gpu->identity.minor_features1; |
| 69 | break; |
| 70 | |
| 71 | case ETNAVIV_PARAM_GPU_FEATURES_3: |
| 72 | *value = gpu->identity.minor_features2; |
| 73 | break; |
| 74 | |
| 75 | case ETNAVIV_PARAM_GPU_FEATURES_4: |
| 76 | *value = gpu->identity.minor_features3; |
| 77 | break; |
| 78 | |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 79 | case ETNAVIV_PARAM_GPU_FEATURES_5: |
| 80 | *value = gpu->identity.minor_features4; |
| 81 | break; |
| 82 | |
| 83 | case ETNAVIV_PARAM_GPU_FEATURES_6: |
| 84 | *value = gpu->identity.minor_features5; |
| 85 | break; |
| 86 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 87 | case ETNAVIV_PARAM_GPU_STREAM_COUNT: |
| 88 | *value = gpu->identity.stream_count; |
| 89 | break; |
| 90 | |
| 91 | case ETNAVIV_PARAM_GPU_REGISTER_MAX: |
| 92 | *value = gpu->identity.register_max; |
| 93 | break; |
| 94 | |
| 95 | case ETNAVIV_PARAM_GPU_THREAD_COUNT: |
| 96 | *value = gpu->identity.thread_count; |
| 97 | break; |
| 98 | |
| 99 | case ETNAVIV_PARAM_GPU_VERTEX_CACHE_SIZE: |
| 100 | *value = gpu->identity.vertex_cache_size; |
| 101 | break; |
| 102 | |
| 103 | case ETNAVIV_PARAM_GPU_SHADER_CORE_COUNT: |
| 104 | *value = gpu->identity.shader_core_count; |
| 105 | break; |
| 106 | |
| 107 | case ETNAVIV_PARAM_GPU_PIXEL_PIPES: |
| 108 | *value = gpu->identity.pixel_pipes; |
| 109 | break; |
| 110 | |
| 111 | case ETNAVIV_PARAM_GPU_VERTEX_OUTPUT_BUFFER_SIZE: |
| 112 | *value = gpu->identity.vertex_output_buffer_size; |
| 113 | break; |
| 114 | |
| 115 | case ETNAVIV_PARAM_GPU_BUFFER_SIZE: |
| 116 | *value = gpu->identity.buffer_size; |
| 117 | break; |
| 118 | |
| 119 | case ETNAVIV_PARAM_GPU_INSTRUCTION_COUNT: |
| 120 | *value = gpu->identity.instruction_count; |
| 121 | break; |
| 122 | |
| 123 | case ETNAVIV_PARAM_GPU_NUM_CONSTANTS: |
| 124 | *value = gpu->identity.num_constants; |
| 125 | break; |
| 126 | |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 127 | case ETNAVIV_PARAM_GPU_NUM_VARYINGS: |
| 128 | *value = gpu->identity.varyings_count; |
| 129 | break; |
| 130 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 131 | default: |
| 132 | DBG("%s: invalid param: %u", dev_name(gpu->dev), param); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Russell King | 472f79d | 2016-01-24 17:35:59 +0000 | [diff] [blame] | 139 | |
| 140 | #define etnaviv_is_model_rev(gpu, mod, rev) \ |
| 141 | ((gpu)->identity.model == chipModel_##mod && \ |
| 142 | (gpu)->identity.revision == rev) |
Russell King | 52f36ba | 2016-01-24 17:35:54 +0000 | [diff] [blame] | 143 | #define etnaviv_field(val, field) \ |
| 144 | (((val) & field##__MASK) >> field##__SHIFT) |
| 145 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 146 | static void etnaviv_hw_specs(struct etnaviv_gpu *gpu) |
| 147 | { |
| 148 | if (gpu->identity.minor_features0 & |
| 149 | chipMinorFeatures0_MORE_MINOR_FEATURES) { |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 150 | u32 specs[4]; |
| 151 | unsigned int streams; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 152 | |
| 153 | specs[0] = gpu_read(gpu, VIVS_HI_CHIP_SPECS); |
| 154 | specs[1] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_2); |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 155 | specs[2] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_3); |
| 156 | specs[3] = gpu_read(gpu, VIVS_HI_CHIP_SPECS_4); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 157 | |
Russell King | 52f36ba | 2016-01-24 17:35:54 +0000 | [diff] [blame] | 158 | gpu->identity.stream_count = etnaviv_field(specs[0], |
| 159 | VIVS_HI_CHIP_SPECS_STREAM_COUNT); |
| 160 | gpu->identity.register_max = etnaviv_field(specs[0], |
| 161 | VIVS_HI_CHIP_SPECS_REGISTER_MAX); |
| 162 | gpu->identity.thread_count = etnaviv_field(specs[0], |
| 163 | VIVS_HI_CHIP_SPECS_THREAD_COUNT); |
| 164 | gpu->identity.vertex_cache_size = etnaviv_field(specs[0], |
| 165 | VIVS_HI_CHIP_SPECS_VERTEX_CACHE_SIZE); |
| 166 | gpu->identity.shader_core_count = etnaviv_field(specs[0], |
| 167 | VIVS_HI_CHIP_SPECS_SHADER_CORE_COUNT); |
| 168 | gpu->identity.pixel_pipes = etnaviv_field(specs[0], |
| 169 | VIVS_HI_CHIP_SPECS_PIXEL_PIPES); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 170 | gpu->identity.vertex_output_buffer_size = |
Russell King | 52f36ba | 2016-01-24 17:35:54 +0000 | [diff] [blame] | 171 | etnaviv_field(specs[0], |
| 172 | VIVS_HI_CHIP_SPECS_VERTEX_OUTPUT_BUFFER_SIZE); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 173 | |
Russell King | 52f36ba | 2016-01-24 17:35:54 +0000 | [diff] [blame] | 174 | gpu->identity.buffer_size = etnaviv_field(specs[1], |
| 175 | VIVS_HI_CHIP_SPECS_2_BUFFER_SIZE); |
| 176 | gpu->identity.instruction_count = etnaviv_field(specs[1], |
| 177 | VIVS_HI_CHIP_SPECS_2_INSTRUCTION_COUNT); |
| 178 | gpu->identity.num_constants = etnaviv_field(specs[1], |
| 179 | VIVS_HI_CHIP_SPECS_2_NUM_CONSTANTS); |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 180 | |
| 181 | gpu->identity.varyings_count = etnaviv_field(specs[2], |
| 182 | VIVS_HI_CHIP_SPECS_3_VARYINGS_COUNT); |
| 183 | |
| 184 | /* This overrides the value from older register if non-zero */ |
| 185 | streams = etnaviv_field(specs[3], |
| 186 | VIVS_HI_CHIP_SPECS_4_STREAM_COUNT); |
| 187 | if (streams) |
| 188 | gpu->identity.stream_count = streams; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* Fill in the stream count if not specified */ |
| 192 | if (gpu->identity.stream_count == 0) { |
| 193 | if (gpu->identity.model >= 0x1000) |
| 194 | gpu->identity.stream_count = 4; |
| 195 | else |
| 196 | gpu->identity.stream_count = 1; |
| 197 | } |
| 198 | |
| 199 | /* Convert the register max value */ |
| 200 | if (gpu->identity.register_max) |
| 201 | gpu->identity.register_max = 1 << gpu->identity.register_max; |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 202 | else if (gpu->identity.model == chipModel_GC400) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 203 | gpu->identity.register_max = 32; |
| 204 | else |
| 205 | gpu->identity.register_max = 64; |
| 206 | |
| 207 | /* Convert thread count */ |
| 208 | if (gpu->identity.thread_count) |
| 209 | gpu->identity.thread_count = 1 << gpu->identity.thread_count; |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 210 | else if (gpu->identity.model == chipModel_GC400) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 211 | gpu->identity.thread_count = 64; |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 212 | else if (gpu->identity.model == chipModel_GC500 || |
| 213 | gpu->identity.model == chipModel_GC530) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 214 | gpu->identity.thread_count = 128; |
| 215 | else |
| 216 | gpu->identity.thread_count = 256; |
| 217 | |
| 218 | if (gpu->identity.vertex_cache_size == 0) |
| 219 | gpu->identity.vertex_cache_size = 8; |
| 220 | |
| 221 | if (gpu->identity.shader_core_count == 0) { |
| 222 | if (gpu->identity.model >= 0x1000) |
| 223 | gpu->identity.shader_core_count = 2; |
| 224 | else |
| 225 | gpu->identity.shader_core_count = 1; |
| 226 | } |
| 227 | |
| 228 | if (gpu->identity.pixel_pipes == 0) |
| 229 | gpu->identity.pixel_pipes = 1; |
| 230 | |
| 231 | /* Convert virtex buffer size */ |
| 232 | if (gpu->identity.vertex_output_buffer_size) { |
| 233 | gpu->identity.vertex_output_buffer_size = |
| 234 | 1 << gpu->identity.vertex_output_buffer_size; |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 235 | } else if (gpu->identity.model == chipModel_GC400) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 236 | if (gpu->identity.revision < 0x4000) |
| 237 | gpu->identity.vertex_output_buffer_size = 512; |
| 238 | else if (gpu->identity.revision < 0x4200) |
| 239 | gpu->identity.vertex_output_buffer_size = 256; |
| 240 | else |
| 241 | gpu->identity.vertex_output_buffer_size = 128; |
| 242 | } else { |
| 243 | gpu->identity.vertex_output_buffer_size = 512; |
| 244 | } |
| 245 | |
| 246 | switch (gpu->identity.instruction_count) { |
| 247 | case 0: |
Russell King | 472f79d | 2016-01-24 17:35:59 +0000 | [diff] [blame] | 248 | if (etnaviv_is_model_rev(gpu, GC2000, 0x5108) || |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 249 | gpu->identity.model == chipModel_GC880) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 250 | gpu->identity.instruction_count = 512; |
| 251 | else |
| 252 | gpu->identity.instruction_count = 256; |
| 253 | break; |
| 254 | |
| 255 | case 1: |
| 256 | gpu->identity.instruction_count = 1024; |
| 257 | break; |
| 258 | |
| 259 | case 2: |
| 260 | gpu->identity.instruction_count = 2048; |
| 261 | break; |
| 262 | |
| 263 | default: |
| 264 | gpu->identity.instruction_count = 256; |
| 265 | break; |
| 266 | } |
| 267 | |
| 268 | if (gpu->identity.num_constants == 0) |
| 269 | gpu->identity.num_constants = 168; |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 270 | |
| 271 | if (gpu->identity.varyings_count == 0) { |
| 272 | if (gpu->identity.minor_features1 & chipMinorFeatures1_HALTI0) |
| 273 | gpu->identity.varyings_count = 12; |
| 274 | else |
| 275 | gpu->identity.varyings_count = 8; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * For some cores, two varyings are consumed for position, so the |
| 280 | * maximum varying count needs to be reduced by one. |
| 281 | */ |
| 282 | if (etnaviv_is_model_rev(gpu, GC5000, 0x5434) || |
| 283 | etnaviv_is_model_rev(gpu, GC4000, 0x5222) || |
| 284 | etnaviv_is_model_rev(gpu, GC4000, 0x5245) || |
| 285 | etnaviv_is_model_rev(gpu, GC4000, 0x5208) || |
| 286 | etnaviv_is_model_rev(gpu, GC3000, 0x5435) || |
| 287 | etnaviv_is_model_rev(gpu, GC2200, 0x5244) || |
| 288 | etnaviv_is_model_rev(gpu, GC2100, 0x5108) || |
| 289 | etnaviv_is_model_rev(gpu, GC2000, 0x5108) || |
| 290 | etnaviv_is_model_rev(gpu, GC1500, 0x5246) || |
| 291 | etnaviv_is_model_rev(gpu, GC880, 0x5107) || |
| 292 | etnaviv_is_model_rev(gpu, GC880, 0x5106)) |
| 293 | gpu->identity.varyings_count -= 1; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static void etnaviv_hw_identify(struct etnaviv_gpu *gpu) |
| 297 | { |
| 298 | u32 chipIdentity; |
| 299 | |
| 300 | chipIdentity = gpu_read(gpu, VIVS_HI_CHIP_IDENTITY); |
| 301 | |
| 302 | /* Special case for older graphic cores. */ |
Russell King | 52f36ba | 2016-01-24 17:35:54 +0000 | [diff] [blame] | 303 | if (etnaviv_field(chipIdentity, VIVS_HI_CHIP_IDENTITY_FAMILY) == 0x01) { |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 304 | gpu->identity.model = chipModel_GC500; |
Russell King | 52f36ba | 2016-01-24 17:35:54 +0000 | [diff] [blame] | 305 | gpu->identity.revision = etnaviv_field(chipIdentity, |
| 306 | VIVS_HI_CHIP_IDENTITY_REVISION); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 307 | } else { |
| 308 | |
| 309 | gpu->identity.model = gpu_read(gpu, VIVS_HI_CHIP_MODEL); |
| 310 | gpu->identity.revision = gpu_read(gpu, VIVS_HI_CHIP_REV); |
| 311 | |
| 312 | /* |
| 313 | * !!!! HACK ALERT !!!! |
| 314 | * Because people change device IDs without letting software |
| 315 | * know about it - here is the hack to make it all look the |
| 316 | * same. Only for GC400 family. |
| 317 | */ |
| 318 | if ((gpu->identity.model & 0xff00) == 0x0400 && |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 319 | gpu->identity.model != chipModel_GC420) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 320 | gpu->identity.model = gpu->identity.model & 0x0400; |
| 321 | } |
| 322 | |
| 323 | /* Another special case */ |
Russell King | 472f79d | 2016-01-24 17:35:59 +0000 | [diff] [blame] | 324 | if (etnaviv_is_model_rev(gpu, GC300, 0x2201)) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 325 | u32 chipDate = gpu_read(gpu, VIVS_HI_CHIP_DATE); |
| 326 | u32 chipTime = gpu_read(gpu, VIVS_HI_CHIP_TIME); |
| 327 | |
| 328 | if (chipDate == 0x20080814 && chipTime == 0x12051100) { |
| 329 | /* |
| 330 | * This IP has an ECO; put the correct |
| 331 | * revision in it. |
| 332 | */ |
| 333 | gpu->identity.revision = 0x1051; |
| 334 | } |
| 335 | } |
Lucas Stach | 12ff4bd | 2016-08-15 18:16:59 +0200 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * NXP likes to call the GPU on the i.MX6QP GC2000+, but in |
| 339 | * reality it's just a re-branded GC3000. We can identify this |
| 340 | * core by the upper half of the revision register being all 1. |
| 341 | * Fix model/rev here, so all other places can refer to this |
| 342 | * core by its real identity. |
| 343 | */ |
| 344 | if (etnaviv_is_model_rev(gpu, GC2000, 0xffff5450)) { |
| 345 | gpu->identity.model = chipModel_GC3000; |
| 346 | gpu->identity.revision &= 0xffff; |
| 347 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | dev_info(gpu->dev, "model: GC%x, revision: %x\n", |
| 351 | gpu->identity.model, gpu->identity.revision); |
| 352 | |
| 353 | gpu->identity.features = gpu_read(gpu, VIVS_HI_CHIP_FEATURE); |
| 354 | |
| 355 | /* Disable fast clear on GC700. */ |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 356 | if (gpu->identity.model == chipModel_GC700) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 357 | gpu->identity.features &= ~chipFeatures_FAST_CLEAR; |
| 358 | |
Russell King | 507f899 | 2016-01-24 17:35:48 +0000 | [diff] [blame] | 359 | if ((gpu->identity.model == chipModel_GC500 && |
| 360 | gpu->identity.revision < 2) || |
| 361 | (gpu->identity.model == chipModel_GC300 && |
| 362 | gpu->identity.revision < 0x2000)) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 363 | |
| 364 | /* |
| 365 | * GC500 rev 1.x and GC300 rev < 2.0 doesn't have these |
| 366 | * registers. |
| 367 | */ |
| 368 | gpu->identity.minor_features0 = 0; |
| 369 | gpu->identity.minor_features1 = 0; |
| 370 | gpu->identity.minor_features2 = 0; |
| 371 | gpu->identity.minor_features3 = 0; |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 372 | gpu->identity.minor_features4 = 0; |
| 373 | gpu->identity.minor_features5 = 0; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 374 | } else |
| 375 | gpu->identity.minor_features0 = |
| 376 | gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_0); |
| 377 | |
| 378 | if (gpu->identity.minor_features0 & |
| 379 | chipMinorFeatures0_MORE_MINOR_FEATURES) { |
| 380 | gpu->identity.minor_features1 = |
| 381 | gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_1); |
| 382 | gpu->identity.minor_features2 = |
| 383 | gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_2); |
| 384 | gpu->identity.minor_features3 = |
| 385 | gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_3); |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 386 | gpu->identity.minor_features4 = |
| 387 | gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_4); |
| 388 | gpu->identity.minor_features5 = |
| 389 | gpu_read(gpu, VIVS_HI_CHIP_MINOR_FEATURE_5); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | /* GC600 idle register reports zero bits where modules aren't present */ |
| 393 | if (gpu->identity.model == chipModel_GC600) { |
| 394 | gpu->idle_mask = VIVS_HI_IDLE_STATE_TX | |
| 395 | VIVS_HI_IDLE_STATE_RA | |
| 396 | VIVS_HI_IDLE_STATE_SE | |
| 397 | VIVS_HI_IDLE_STATE_PA | |
| 398 | VIVS_HI_IDLE_STATE_SH | |
| 399 | VIVS_HI_IDLE_STATE_PE | |
| 400 | VIVS_HI_IDLE_STATE_DE | |
| 401 | VIVS_HI_IDLE_STATE_FE; |
| 402 | } else { |
| 403 | gpu->idle_mask = ~VIVS_HI_IDLE_STATE_AXI_LP; |
| 404 | } |
| 405 | |
| 406 | etnaviv_hw_specs(gpu); |
| 407 | } |
| 408 | |
| 409 | static void etnaviv_gpu_load_clock(struct etnaviv_gpu *gpu, u32 clock) |
| 410 | { |
| 411 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock | |
| 412 | VIVS_HI_CLOCK_CONTROL_FSCALE_CMD_LOAD); |
| 413 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, clock); |
| 414 | } |
| 415 | |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 416 | static void etnaviv_gpu_update_clock(struct etnaviv_gpu *gpu) |
| 417 | { |
Lucas Stach | d79fd1ccf2 | 2017-04-11 15:54:50 +0200 | [diff] [blame] | 418 | if (gpu->identity.minor_features2 & |
| 419 | chipMinorFeatures2_DYNAMIC_FREQUENCY_SCALING) { |
| 420 | clk_set_rate(gpu->clk_core, |
| 421 | gpu->base_rate_core >> gpu->freq_scale); |
| 422 | clk_set_rate(gpu->clk_shader, |
| 423 | gpu->base_rate_shader >> gpu->freq_scale); |
| 424 | } else { |
| 425 | unsigned int fscale = 1 << (6 - gpu->freq_scale); |
Lucas Stach | 6eb3ecc | 2017-09-28 15:41:21 +0200 | [diff] [blame] | 426 | u32 clock = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL); |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 427 | |
Lucas Stach | 6eb3ecc | 2017-09-28 15:41:21 +0200 | [diff] [blame] | 428 | clock &= ~VIVS_HI_CLOCK_CONTROL_FSCALE_VAL__MASK; |
| 429 | clock |= VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale); |
Lucas Stach | d79fd1ccf2 | 2017-04-11 15:54:50 +0200 | [diff] [blame] | 430 | etnaviv_gpu_load_clock(gpu, clock); |
| 431 | } |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 432 | } |
| 433 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 434 | static int etnaviv_hw_reset(struct etnaviv_gpu *gpu) |
| 435 | { |
| 436 | u32 control, idle; |
| 437 | unsigned long timeout; |
| 438 | bool failed = true; |
| 439 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 440 | /* We hope that the GPU resets in under one second */ |
| 441 | timeout = jiffies + msecs_to_jiffies(1000); |
| 442 | |
| 443 | while (time_is_after_jiffies(timeout)) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 444 | /* enable clock */ |
Lucas Stach | 6eb3ecc | 2017-09-28 15:41:21 +0200 | [diff] [blame] | 445 | unsigned int fscale = 1 << (6 - gpu->freq_scale); |
| 446 | control = VIVS_HI_CLOCK_CONTROL_FSCALE_VAL(fscale); |
| 447 | etnaviv_gpu_load_clock(gpu, control); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 448 | |
| 449 | /* isolate the GPU. */ |
| 450 | control |= VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU; |
| 451 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control); |
| 452 | |
| 453 | /* set soft reset. */ |
| 454 | control |= VIVS_HI_CLOCK_CONTROL_SOFT_RESET; |
| 455 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control); |
| 456 | |
| 457 | /* wait for reset. */ |
Philipp Zabel | 4046217 | 2017-10-09 12:03:30 +0200 | [diff] [blame] | 458 | usleep_range(10, 20); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 459 | |
| 460 | /* reset soft reset bit. */ |
| 461 | control &= ~VIVS_HI_CLOCK_CONTROL_SOFT_RESET; |
| 462 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control); |
| 463 | |
| 464 | /* reset GPU isolation. */ |
| 465 | control &= ~VIVS_HI_CLOCK_CONTROL_ISOLATE_GPU; |
| 466 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control); |
| 467 | |
| 468 | /* read idle register. */ |
| 469 | idle = gpu_read(gpu, VIVS_HI_IDLE_STATE); |
| 470 | |
| 471 | /* try reseting again if FE it not idle */ |
| 472 | if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) { |
| 473 | dev_dbg(gpu->dev, "FE is not idle\n"); |
| 474 | continue; |
| 475 | } |
| 476 | |
| 477 | /* read reset register. */ |
| 478 | control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL); |
| 479 | |
| 480 | /* is the GPU idle? */ |
| 481 | if (((control & VIVS_HI_CLOCK_CONTROL_IDLE_3D) == 0) || |
| 482 | ((control & VIVS_HI_CLOCK_CONTROL_IDLE_2D) == 0)) { |
| 483 | dev_dbg(gpu->dev, "GPU is not idle\n"); |
| 484 | continue; |
| 485 | } |
| 486 | |
Lucas Stach | 6eb3ecc | 2017-09-28 15:41:21 +0200 | [diff] [blame] | 487 | /* disable debug registers, as they are not normally needed */ |
| 488 | control |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS; |
| 489 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, control); |
| 490 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 491 | failed = false; |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | if (failed) { |
| 496 | idle = gpu_read(gpu, VIVS_HI_IDLE_STATE); |
| 497 | control = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL); |
| 498 | |
| 499 | dev_err(gpu->dev, "GPU failed to reset: FE %sidle, 3D %sidle, 2D %sidle\n", |
| 500 | idle & VIVS_HI_IDLE_STATE_FE ? "" : "not ", |
| 501 | control & VIVS_HI_CLOCK_CONTROL_IDLE_3D ? "" : "not ", |
| 502 | control & VIVS_HI_CLOCK_CONTROL_IDLE_2D ? "" : "not "); |
| 503 | |
| 504 | return -EBUSY; |
| 505 | } |
| 506 | |
| 507 | /* We rely on the GPU running, so program the clock */ |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 508 | etnaviv_gpu_update_clock(gpu); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
Russell King | 7d0c6e7 | 2016-01-21 15:20:45 +0000 | [diff] [blame] | 513 | static void etnaviv_gpu_enable_mlcg(struct etnaviv_gpu *gpu) |
| 514 | { |
| 515 | u32 pmc, ppc; |
| 516 | |
| 517 | /* enable clock gating */ |
| 518 | ppc = gpu_read(gpu, VIVS_PM_POWER_CONTROLS); |
| 519 | ppc |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING; |
| 520 | |
| 521 | /* Disable stall module clock gating for 4.3.0.1 and 4.3.0.2 revs */ |
| 522 | if (gpu->identity.revision == 0x4301 || |
| 523 | gpu->identity.revision == 0x4302) |
| 524 | ppc |= VIVS_PM_POWER_CONTROLS_DISABLE_STALL_MODULE_CLOCK_GATING; |
| 525 | |
| 526 | gpu_write(gpu, VIVS_PM_POWER_CONTROLS, ppc); |
| 527 | |
| 528 | pmc = gpu_read(gpu, VIVS_PM_MODULE_CONTROLS); |
| 529 | |
Lucas Stach | 7cef600 | 2017-03-17 12:42:30 +0100 | [diff] [blame] | 530 | /* Disable PA clock gating for GC400+ without bugfix except for GC420 */ |
Russell King | 7d0c6e7 | 2016-01-21 15:20:45 +0000 | [diff] [blame] | 531 | if (gpu->identity.model >= chipModel_GC400 && |
Lucas Stach | 7cef600 | 2017-03-17 12:42:30 +0100 | [diff] [blame] | 532 | gpu->identity.model != chipModel_GC420 && |
| 533 | !(gpu->identity.minor_features3 & chipMinorFeatures3_BUG_FIXES12)) |
Russell King | 7d0c6e7 | 2016-01-21 15:20:45 +0000 | [diff] [blame] | 534 | pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PA; |
| 535 | |
| 536 | /* |
| 537 | * Disable PE clock gating on revs < 5.0.0.0 when HZ is |
| 538 | * present without a bug fix. |
| 539 | */ |
| 540 | if (gpu->identity.revision < 0x5000 && |
| 541 | gpu->identity.minor_features0 & chipMinorFeatures0_HZ && |
| 542 | !(gpu->identity.minor_features1 & |
| 543 | chipMinorFeatures1_DISABLE_PE_GATING)) |
| 544 | pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_PE; |
| 545 | |
| 546 | if (gpu->identity.revision < 0x5422) |
| 547 | pmc |= BIT(15); /* Unknown bit */ |
| 548 | |
Lucas Stach | 7cef600 | 2017-03-17 12:42:30 +0100 | [diff] [blame] | 549 | /* Disable TX clock gating on affected core revisions. */ |
| 550 | if (etnaviv_is_model_rev(gpu, GC4000, 0x5222) || |
| 551 | etnaviv_is_model_rev(gpu, GC2000, 0x5108)) |
| 552 | pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_TX; |
| 553 | |
Russell King | 7d0c6e7 | 2016-01-21 15:20:45 +0000 | [diff] [blame] | 554 | pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_HZ; |
| 555 | pmc |= VIVS_PM_MODULE_CONTROLS_DISABLE_MODULE_CLOCK_GATING_RA_EZ; |
| 556 | |
| 557 | gpu_write(gpu, VIVS_PM_MODULE_CONTROLS, pmc); |
| 558 | } |
| 559 | |
Lucas Stach | 229855b | 2016-08-17 15:27:52 +0200 | [diff] [blame] | 560 | void etnaviv_gpu_start_fe(struct etnaviv_gpu *gpu, u32 address, u16 prefetch) |
| 561 | { |
| 562 | gpu_write(gpu, VIVS_FE_COMMAND_ADDRESS, address); |
| 563 | gpu_write(gpu, VIVS_FE_COMMAND_CONTROL, |
| 564 | VIVS_FE_COMMAND_CONTROL_ENABLE | |
| 565 | VIVS_FE_COMMAND_CONTROL_PREFETCH(prefetch)); |
| 566 | } |
| 567 | |
Wladimir J. van der Laan | e17a0de | 2016-12-15 13:11:30 +0100 | [diff] [blame] | 568 | static void etnaviv_gpu_setup_pulse_eater(struct etnaviv_gpu *gpu) |
| 569 | { |
| 570 | /* |
| 571 | * Base value for VIVS_PM_PULSE_EATER register on models where it |
| 572 | * cannot be read, extracted from vivante kernel driver. |
| 573 | */ |
| 574 | u32 pulse_eater = 0x01590880; |
| 575 | |
| 576 | if (etnaviv_is_model_rev(gpu, GC4000, 0x5208) || |
| 577 | etnaviv_is_model_rev(gpu, GC4000, 0x5222)) { |
| 578 | pulse_eater |= BIT(23); |
| 579 | |
| 580 | } |
| 581 | |
| 582 | if (etnaviv_is_model_rev(gpu, GC1000, 0x5039) || |
| 583 | etnaviv_is_model_rev(gpu, GC1000, 0x5040)) { |
| 584 | pulse_eater &= ~BIT(16); |
| 585 | pulse_eater |= BIT(17); |
| 586 | } |
| 587 | |
| 588 | if ((gpu->identity.revision > 0x5420) && |
| 589 | (gpu->identity.features & chipFeatures_PIPE_3D)) |
| 590 | { |
| 591 | /* Performance fix: disable internal DFS */ |
| 592 | pulse_eater = gpu_read(gpu, VIVS_PM_PULSE_EATER); |
| 593 | pulse_eater |= BIT(18); |
| 594 | } |
| 595 | |
| 596 | gpu_write(gpu, VIVS_PM_PULSE_EATER, pulse_eater); |
| 597 | } |
| 598 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 599 | static void etnaviv_gpu_hw_init(struct etnaviv_gpu *gpu) |
| 600 | { |
| 601 | u16 prefetch; |
| 602 | |
Russell King | 472f79d | 2016-01-24 17:35:59 +0000 | [diff] [blame] | 603 | if ((etnaviv_is_model_rev(gpu, GC320, 0x5007) || |
| 604 | etnaviv_is_model_rev(gpu, GC320, 0x5220)) && |
| 605 | gpu_read(gpu, VIVS_HI_CHIP_TIME) != 0x2062400) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 606 | u32 mc_memory_debug; |
| 607 | |
| 608 | mc_memory_debug = gpu_read(gpu, VIVS_MC_DEBUG_MEMORY) & ~0xff; |
| 609 | |
| 610 | if (gpu->identity.revision == 0x5007) |
| 611 | mc_memory_debug |= 0x0c; |
| 612 | else |
| 613 | mc_memory_debug |= 0x08; |
| 614 | |
| 615 | gpu_write(gpu, VIVS_MC_DEBUG_MEMORY, mc_memory_debug); |
| 616 | } |
| 617 | |
Russell King | 7d0c6e7 | 2016-01-21 15:20:45 +0000 | [diff] [blame] | 618 | /* enable module-level clock gating */ |
| 619 | etnaviv_gpu_enable_mlcg(gpu); |
| 620 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 621 | /* |
| 622 | * Update GPU AXI cache atttribute to "cacheable, no allocate". |
| 623 | * This is necessary to prevent the iMX6 SoC locking up. |
| 624 | */ |
| 625 | gpu_write(gpu, VIVS_HI_AXI_CONFIG, |
| 626 | VIVS_HI_AXI_CONFIG_AWCACHE(2) | |
| 627 | VIVS_HI_AXI_CONFIG_ARCACHE(2)); |
| 628 | |
| 629 | /* GC2000 rev 5108 needs a special bus config */ |
Russell King | 472f79d | 2016-01-24 17:35:59 +0000 | [diff] [blame] | 630 | if (etnaviv_is_model_rev(gpu, GC2000, 0x5108)) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 631 | u32 bus_config = gpu_read(gpu, VIVS_MC_BUS_CONFIG); |
| 632 | bus_config &= ~(VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG__MASK | |
| 633 | VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG__MASK); |
| 634 | bus_config |= VIVS_MC_BUS_CONFIG_FE_BUS_CONFIG(1) | |
| 635 | VIVS_MC_BUS_CONFIG_TX_BUS_CONFIG(0); |
| 636 | gpu_write(gpu, VIVS_MC_BUS_CONFIG, bus_config); |
| 637 | } |
| 638 | |
Wladimir J. van der Laan | e17a0de | 2016-12-15 13:11:30 +0100 | [diff] [blame] | 639 | /* setup the pulse eater */ |
| 640 | etnaviv_gpu_setup_pulse_eater(gpu); |
| 641 | |
Lucas Stach | 99f861b | 2016-08-16 11:48:49 +0200 | [diff] [blame] | 642 | /* setup the MMU */ |
Lucas Stach | e095c8f | 2016-08-16 11:54:51 +0200 | [diff] [blame] | 643 | etnaviv_iommu_restore(gpu); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 644 | |
| 645 | /* Start command processor */ |
| 646 | prefetch = etnaviv_buffer_init(gpu); |
| 647 | |
| 648 | gpu_write(gpu, VIVS_HI_INTR_ENBL, ~0U); |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 649 | etnaviv_gpu_start_fe(gpu, etnaviv_cmdbuf_get_va(&gpu->buffer), |
Lucas Stach | 229855b | 2016-08-17 15:27:52 +0200 | [diff] [blame] | 650 | prefetch); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | int etnaviv_gpu_init(struct etnaviv_gpu *gpu) |
| 654 | { |
| 655 | int ret, i; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 656 | |
| 657 | ret = pm_runtime_get_sync(gpu->dev); |
Lucas Stach | 1409df0 | 2016-06-17 12:29:02 +0200 | [diff] [blame] | 658 | if (ret < 0) { |
| 659 | dev_err(gpu->dev, "Failed to enable GPU power domain\n"); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 660 | return ret; |
Lucas Stach | 1409df0 | 2016-06-17 12:29:02 +0200 | [diff] [blame] | 661 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 662 | |
| 663 | etnaviv_hw_identify(gpu); |
| 664 | |
| 665 | if (gpu->identity.model == 0) { |
| 666 | dev_err(gpu->dev, "Unknown GPU model\n"); |
Russell King | f642776 | 2016-01-24 17:32:13 +0000 | [diff] [blame] | 667 | ret = -ENXIO; |
| 668 | goto fail; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 669 | } |
| 670 | |
Russell King | b98c668 | 2016-01-21 15:19:59 +0000 | [diff] [blame] | 671 | /* Exclude VG cores with FE2.0 */ |
| 672 | if (gpu->identity.features & chipFeatures_PIPE_VG && |
| 673 | gpu->identity.features & chipFeatures_FE20) { |
| 674 | dev_info(gpu->dev, "Ignoring GPU with VG and FE2.0\n"); |
| 675 | ret = -ENXIO; |
| 676 | goto fail; |
| 677 | } |
| 678 | |
Lucas Stach | 2144fff | 2016-04-21 13:52:38 +0200 | [diff] [blame] | 679 | /* |
| 680 | * Set the GPU linear window to be at the end of the DMA window, where |
| 681 | * the CMA area is likely to reside. This ensures that we are able to |
| 682 | * map the command buffers while having the linear window overlap as |
| 683 | * much RAM as possible, so we can optimize mappings for other buffers. |
| 684 | * |
| 685 | * For 3D cores only do this if MC2.0 is present, as with MC1.0 it leads |
| 686 | * to different views of the memory on the individual engines. |
| 687 | */ |
| 688 | if (!(gpu->identity.features & chipFeatures_PIPE_3D) || |
| 689 | (gpu->identity.minor_features0 & chipMinorFeatures0_MC20)) { |
| 690 | u32 dma_mask = (u32)dma_get_required_mask(gpu->dev); |
| 691 | if (dma_mask < PHYS_OFFSET + SZ_2G) |
| 692 | gpu->memory_base = PHYS_OFFSET; |
| 693 | else |
| 694 | gpu->memory_base = dma_mask - SZ_2G + 1; |
Lucas Stach | 1db0127 | 2016-12-02 12:19:16 +0100 | [diff] [blame] | 695 | } else if (PHYS_OFFSET >= SZ_2G) { |
| 696 | dev_info(gpu->dev, "Need to move linear window on MC1.0, disabling TS\n"); |
| 697 | gpu->memory_base = PHYS_OFFSET; |
| 698 | gpu->identity.features &= ~chipFeatures_FAST_CLEAR; |
Lucas Stach | 2144fff | 2016-04-21 13:52:38 +0200 | [diff] [blame] | 699 | } |
| 700 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 701 | ret = etnaviv_hw_reset(gpu); |
Lucas Stach | 1409df0 | 2016-06-17 12:29:02 +0200 | [diff] [blame] | 702 | if (ret) { |
| 703 | dev_err(gpu->dev, "GPU reset failed\n"); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 704 | goto fail; |
Lucas Stach | 1409df0 | 2016-06-17 12:29:02 +0200 | [diff] [blame] | 705 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 706 | |
Lucas Stach | dd34bb9 | 2016-08-16 12:09:08 +0200 | [diff] [blame] | 707 | gpu->mmu = etnaviv_iommu_new(gpu); |
| 708 | if (IS_ERR(gpu->mmu)) { |
Lucas Stach | 1409df0 | 2016-06-17 12:29:02 +0200 | [diff] [blame] | 709 | dev_err(gpu->dev, "Failed to instantiate GPU IOMMU\n"); |
Lucas Stach | dd34bb9 | 2016-08-16 12:09:08 +0200 | [diff] [blame] | 710 | ret = PTR_ERR(gpu->mmu); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 711 | goto fail; |
| 712 | } |
| 713 | |
Lucas Stach | e66774d | 2017-01-16 17:29:57 +0100 | [diff] [blame] | 714 | gpu->cmdbuf_suballoc = etnaviv_cmdbuf_suballoc_new(gpu); |
| 715 | if (IS_ERR(gpu->cmdbuf_suballoc)) { |
| 716 | dev_err(gpu->dev, "Failed to create cmdbuf suballocator\n"); |
| 717 | ret = PTR_ERR(gpu->cmdbuf_suballoc); |
| 718 | goto fail; |
| 719 | } |
| 720 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 721 | /* Create buffer: */ |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 722 | ret = etnaviv_cmdbuf_init(gpu->cmdbuf_suballoc, &gpu->buffer, |
| 723 | PAGE_SIZE); |
| 724 | if (ret) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 725 | dev_err(gpu->dev, "could not create command buffer\n"); |
Lucas Stach | 45d16a6 | 2016-01-25 12:41:05 +0100 | [diff] [blame] | 726 | goto destroy_iommu; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 727 | } |
Lucas Stach | acfee0e | 2016-08-17 16:19:53 +0200 | [diff] [blame] | 728 | |
| 729 | if (gpu->mmu->version == ETNAVIV_IOMMU_V1 && |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 730 | etnaviv_cmdbuf_get_va(&gpu->buffer) > 0x80000000) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 731 | ret = -EINVAL; |
| 732 | dev_err(gpu->dev, |
| 733 | "command buffer outside valid memory window\n"); |
| 734 | goto free_buffer; |
| 735 | } |
| 736 | |
| 737 | /* Setup event management */ |
| 738 | spin_lock_init(&gpu->event_spinlock); |
| 739 | init_completion(&gpu->event_free); |
Christian Gmeiner | 355502e | 2017-09-24 15:15:19 +0200 | [diff] [blame] | 740 | bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS); |
| 741 | for (i = 0; i < ARRAY_SIZE(gpu->event); i++) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 742 | complete(&gpu->event_free); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 743 | |
| 744 | /* Now program the hardware */ |
| 745 | mutex_lock(&gpu->lock); |
| 746 | etnaviv_gpu_hw_init(gpu); |
Russell King | f608631 | 2016-01-21 15:20:19 +0000 | [diff] [blame] | 747 | gpu->exec_state = -1; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 748 | mutex_unlock(&gpu->lock); |
| 749 | |
| 750 | pm_runtime_mark_last_busy(gpu->dev); |
| 751 | pm_runtime_put_autosuspend(gpu->dev); |
| 752 | |
| 753 | return 0; |
| 754 | |
| 755 | free_buffer: |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 756 | etnaviv_cmdbuf_free(&gpu->buffer); |
Lucas Stach | 45d16a6 | 2016-01-25 12:41:05 +0100 | [diff] [blame] | 757 | destroy_iommu: |
| 758 | etnaviv_iommu_destroy(gpu->mmu); |
| 759 | gpu->mmu = NULL; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 760 | fail: |
| 761 | pm_runtime_mark_last_busy(gpu->dev); |
| 762 | pm_runtime_put_autosuspend(gpu->dev); |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | #ifdef CONFIG_DEBUG_FS |
| 768 | struct dma_debug { |
| 769 | u32 address[2]; |
| 770 | u32 state[2]; |
| 771 | }; |
| 772 | |
| 773 | static void verify_dma(struct etnaviv_gpu *gpu, struct dma_debug *debug) |
| 774 | { |
| 775 | u32 i; |
| 776 | |
| 777 | debug->address[0] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS); |
| 778 | debug->state[0] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE); |
| 779 | |
| 780 | for (i = 0; i < 500; i++) { |
| 781 | debug->address[1] = gpu_read(gpu, VIVS_FE_DMA_ADDRESS); |
| 782 | debug->state[1] = gpu_read(gpu, VIVS_FE_DMA_DEBUG_STATE); |
| 783 | |
| 784 | if (debug->address[0] != debug->address[1]) |
| 785 | break; |
| 786 | |
| 787 | if (debug->state[0] != debug->state[1]) |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | int etnaviv_gpu_debugfs(struct etnaviv_gpu *gpu, struct seq_file *m) |
| 793 | { |
| 794 | struct dma_debug debug; |
| 795 | u32 dma_lo, dma_hi, axi, idle; |
| 796 | int ret; |
| 797 | |
| 798 | seq_printf(m, "%s Status:\n", dev_name(gpu->dev)); |
| 799 | |
| 800 | ret = pm_runtime_get_sync(gpu->dev); |
| 801 | if (ret < 0) |
| 802 | return ret; |
| 803 | |
| 804 | dma_lo = gpu_read(gpu, VIVS_FE_DMA_LOW); |
| 805 | dma_hi = gpu_read(gpu, VIVS_FE_DMA_HIGH); |
| 806 | axi = gpu_read(gpu, VIVS_HI_AXI_STATUS); |
| 807 | idle = gpu_read(gpu, VIVS_HI_IDLE_STATE); |
| 808 | |
| 809 | verify_dma(gpu, &debug); |
| 810 | |
| 811 | seq_puts(m, "\tfeatures\n"); |
Lucas Stach | 3d9fc64 | 2018-01-04 13:50:14 +0100 | [diff] [blame] | 812 | seq_printf(m, "\t major_features: 0x%08x\n", |
| 813 | gpu->identity.features); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 814 | seq_printf(m, "\t minor_features0: 0x%08x\n", |
| 815 | gpu->identity.minor_features0); |
| 816 | seq_printf(m, "\t minor_features1: 0x%08x\n", |
| 817 | gpu->identity.minor_features1); |
| 818 | seq_printf(m, "\t minor_features2: 0x%08x\n", |
| 819 | gpu->identity.minor_features2); |
| 820 | seq_printf(m, "\t minor_features3: 0x%08x\n", |
| 821 | gpu->identity.minor_features3); |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 822 | seq_printf(m, "\t minor_features4: 0x%08x\n", |
| 823 | gpu->identity.minor_features4); |
| 824 | seq_printf(m, "\t minor_features5: 0x%08x\n", |
| 825 | gpu->identity.minor_features5); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 826 | |
| 827 | seq_puts(m, "\tspecs\n"); |
| 828 | seq_printf(m, "\t stream_count: %d\n", |
| 829 | gpu->identity.stream_count); |
| 830 | seq_printf(m, "\t register_max: %d\n", |
| 831 | gpu->identity.register_max); |
| 832 | seq_printf(m, "\t thread_count: %d\n", |
| 833 | gpu->identity.thread_count); |
| 834 | seq_printf(m, "\t vertex_cache_size: %d\n", |
| 835 | gpu->identity.vertex_cache_size); |
| 836 | seq_printf(m, "\t shader_core_count: %d\n", |
| 837 | gpu->identity.shader_core_count); |
| 838 | seq_printf(m, "\t pixel_pipes: %d\n", |
| 839 | gpu->identity.pixel_pipes); |
| 840 | seq_printf(m, "\t vertex_output_buffer_size: %d\n", |
| 841 | gpu->identity.vertex_output_buffer_size); |
| 842 | seq_printf(m, "\t buffer_size: %d\n", |
| 843 | gpu->identity.buffer_size); |
| 844 | seq_printf(m, "\t instruction_count: %d\n", |
| 845 | gpu->identity.instruction_count); |
| 846 | seq_printf(m, "\t num_constants: %d\n", |
| 847 | gpu->identity.num_constants); |
Russell King | 602eb48 | 2016-01-24 17:36:04 +0000 | [diff] [blame] | 848 | seq_printf(m, "\t varyings_count: %d\n", |
| 849 | gpu->identity.varyings_count); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 850 | |
| 851 | seq_printf(m, "\taxi: 0x%08x\n", axi); |
| 852 | seq_printf(m, "\tidle: 0x%08x\n", idle); |
| 853 | idle |= ~gpu->idle_mask & ~VIVS_HI_IDLE_STATE_AXI_LP; |
| 854 | if ((idle & VIVS_HI_IDLE_STATE_FE) == 0) |
| 855 | seq_puts(m, "\t FE is not idle\n"); |
| 856 | if ((idle & VIVS_HI_IDLE_STATE_DE) == 0) |
| 857 | seq_puts(m, "\t DE is not idle\n"); |
| 858 | if ((idle & VIVS_HI_IDLE_STATE_PE) == 0) |
| 859 | seq_puts(m, "\t PE is not idle\n"); |
| 860 | if ((idle & VIVS_HI_IDLE_STATE_SH) == 0) |
| 861 | seq_puts(m, "\t SH is not idle\n"); |
| 862 | if ((idle & VIVS_HI_IDLE_STATE_PA) == 0) |
| 863 | seq_puts(m, "\t PA is not idle\n"); |
| 864 | if ((idle & VIVS_HI_IDLE_STATE_SE) == 0) |
| 865 | seq_puts(m, "\t SE is not idle\n"); |
| 866 | if ((idle & VIVS_HI_IDLE_STATE_RA) == 0) |
| 867 | seq_puts(m, "\t RA is not idle\n"); |
| 868 | if ((idle & VIVS_HI_IDLE_STATE_TX) == 0) |
| 869 | seq_puts(m, "\t TX is not idle\n"); |
| 870 | if ((idle & VIVS_HI_IDLE_STATE_VG) == 0) |
| 871 | seq_puts(m, "\t VG is not idle\n"); |
| 872 | if ((idle & VIVS_HI_IDLE_STATE_IM) == 0) |
| 873 | seq_puts(m, "\t IM is not idle\n"); |
| 874 | if ((idle & VIVS_HI_IDLE_STATE_FP) == 0) |
| 875 | seq_puts(m, "\t FP is not idle\n"); |
| 876 | if ((idle & VIVS_HI_IDLE_STATE_TS) == 0) |
| 877 | seq_puts(m, "\t TS is not idle\n"); |
| 878 | if (idle & VIVS_HI_IDLE_STATE_AXI_LP) |
| 879 | seq_puts(m, "\t AXI low power mode\n"); |
| 880 | |
| 881 | if (gpu->identity.features & chipFeatures_DEBUG_MODE) { |
| 882 | u32 read0 = gpu_read(gpu, VIVS_MC_DEBUG_READ0); |
| 883 | u32 read1 = gpu_read(gpu, VIVS_MC_DEBUG_READ1); |
| 884 | u32 write = gpu_read(gpu, VIVS_MC_DEBUG_WRITE); |
| 885 | |
| 886 | seq_puts(m, "\tMC\n"); |
| 887 | seq_printf(m, "\t read0: 0x%08x\n", read0); |
| 888 | seq_printf(m, "\t read1: 0x%08x\n", read1); |
| 889 | seq_printf(m, "\t write: 0x%08x\n", write); |
| 890 | } |
| 891 | |
| 892 | seq_puts(m, "\tDMA "); |
| 893 | |
| 894 | if (debug.address[0] == debug.address[1] && |
| 895 | debug.state[0] == debug.state[1]) { |
| 896 | seq_puts(m, "seems to be stuck\n"); |
| 897 | } else if (debug.address[0] == debug.address[1]) { |
Masanari Iida | c01e015 | 2016-04-20 00:27:33 +0900 | [diff] [blame] | 898 | seq_puts(m, "address is constant\n"); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 899 | } else { |
Masanari Iida | c01e015 | 2016-04-20 00:27:33 +0900 | [diff] [blame] | 900 | seq_puts(m, "is running\n"); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | seq_printf(m, "\t address 0: 0x%08x\n", debug.address[0]); |
| 904 | seq_printf(m, "\t address 1: 0x%08x\n", debug.address[1]); |
| 905 | seq_printf(m, "\t state 0: 0x%08x\n", debug.state[0]); |
| 906 | seq_printf(m, "\t state 1: 0x%08x\n", debug.state[1]); |
| 907 | seq_printf(m, "\t last fetch 64 bit word: 0x%08x 0x%08x\n", |
| 908 | dma_lo, dma_hi); |
| 909 | |
| 910 | ret = 0; |
| 911 | |
| 912 | pm_runtime_mark_last_busy(gpu->dev); |
| 913 | pm_runtime_put_autosuspend(gpu->dev); |
| 914 | |
| 915 | return ret; |
| 916 | } |
| 917 | #endif |
| 918 | |
Lucas Stach | 6d7a20c | 2017-12-06 10:53:27 +0100 | [diff] [blame] | 919 | void etnaviv_gpu_recover_hang(struct etnaviv_gpu *gpu) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 920 | { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 921 | unsigned long flags; |
Christian Gmeiner | 355502e | 2017-09-24 15:15:19 +0200 | [diff] [blame] | 922 | unsigned int i = 0; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 923 | |
Lucas Stach | 6d7a20c | 2017-12-06 10:53:27 +0100 | [diff] [blame] | 924 | dev_err(gpu->dev, "recover hung GPU!\n"); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 925 | |
| 926 | if (pm_runtime_get_sync(gpu->dev) < 0) |
| 927 | return; |
| 928 | |
| 929 | mutex_lock(&gpu->lock); |
| 930 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 931 | etnaviv_hw_reset(gpu); |
| 932 | |
| 933 | /* complete all events, the GPU won't do it after the reset */ |
| 934 | spin_lock_irqsave(&gpu->event_spinlock, flags); |
Lucas Stach | 6d7a20c | 2017-12-06 10:53:27 +0100 | [diff] [blame] | 935 | for_each_set_bit_from(i, gpu->event_bitmap, ETNA_NR_EVENTS) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 936 | complete(&gpu->event_free); |
Christian Gmeiner | 355502e | 2017-09-24 15:15:19 +0200 | [diff] [blame] | 937 | bitmap_zero(gpu->event_bitmap, ETNA_NR_EVENTS); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 938 | spin_unlock_irqrestore(&gpu->event_spinlock, flags); |
| 939 | gpu->completed_fence = gpu->active_fence; |
| 940 | |
| 941 | etnaviv_gpu_hw_init(gpu); |
Lucas Stach | 1b94a9b | 2016-09-15 12:57:32 +0200 | [diff] [blame] | 942 | gpu->lastctx = NULL; |
Russell King | f608631 | 2016-01-21 15:20:19 +0000 | [diff] [blame] | 943 | gpu->exec_state = -1; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 944 | |
| 945 | mutex_unlock(&gpu->lock); |
| 946 | pm_runtime_mark_last_busy(gpu->dev); |
| 947 | pm_runtime_put_autosuspend(gpu->dev); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 948 | } |
| 949 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 950 | /* fence object management */ |
| 951 | struct etnaviv_fence { |
| 952 | struct etnaviv_gpu *gpu; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 953 | struct dma_fence base; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 954 | }; |
| 955 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 956 | static inline struct etnaviv_fence *to_etnaviv_fence(struct dma_fence *fence) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 957 | { |
| 958 | return container_of(fence, struct etnaviv_fence, base); |
| 959 | } |
| 960 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 961 | static const char *etnaviv_fence_get_driver_name(struct dma_fence *fence) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 962 | { |
| 963 | return "etnaviv"; |
| 964 | } |
| 965 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 966 | static const char *etnaviv_fence_get_timeline_name(struct dma_fence *fence) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 967 | { |
| 968 | struct etnaviv_fence *f = to_etnaviv_fence(fence); |
| 969 | |
| 970 | return dev_name(f->gpu->dev); |
| 971 | } |
| 972 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 973 | static bool etnaviv_fence_enable_signaling(struct dma_fence *fence) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 974 | { |
| 975 | return true; |
| 976 | } |
| 977 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 978 | static bool etnaviv_fence_signaled(struct dma_fence *fence) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 979 | { |
| 980 | struct etnaviv_fence *f = to_etnaviv_fence(fence); |
| 981 | |
| 982 | return fence_completed(f->gpu, f->base.seqno); |
| 983 | } |
| 984 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 985 | static void etnaviv_fence_release(struct dma_fence *fence) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 986 | { |
| 987 | struct etnaviv_fence *f = to_etnaviv_fence(fence); |
| 988 | |
| 989 | kfree_rcu(f, base.rcu); |
| 990 | } |
| 991 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 992 | static const struct dma_fence_ops etnaviv_fence_ops = { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 993 | .get_driver_name = etnaviv_fence_get_driver_name, |
| 994 | .get_timeline_name = etnaviv_fence_get_timeline_name, |
| 995 | .enable_signaling = etnaviv_fence_enable_signaling, |
| 996 | .signaled = etnaviv_fence_signaled, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 997 | .wait = dma_fence_default_wait, |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 998 | .release = etnaviv_fence_release, |
| 999 | }; |
| 1000 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1001 | static struct dma_fence *etnaviv_gpu_fence_alloc(struct etnaviv_gpu *gpu) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1002 | { |
| 1003 | struct etnaviv_fence *f; |
| 1004 | |
Lucas Stach | b27734c2 | 2017-03-22 12:23:43 +0100 | [diff] [blame] | 1005 | /* |
| 1006 | * GPU lock must already be held, otherwise fence completion order might |
| 1007 | * not match the seqno order assigned here. |
| 1008 | */ |
| 1009 | lockdep_assert_held(&gpu->lock); |
| 1010 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1011 | f = kzalloc(sizeof(*f), GFP_KERNEL); |
| 1012 | if (!f) |
| 1013 | return NULL; |
| 1014 | |
| 1015 | f->gpu = gpu; |
| 1016 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1017 | dma_fence_init(&f->base, &etnaviv_fence_ops, &gpu->fence_spinlock, |
| 1018 | gpu->fence_context, ++gpu->next_fence); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1019 | |
| 1020 | return &f->base; |
| 1021 | } |
| 1022 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1023 | /* |
| 1024 | * event management: |
| 1025 | */ |
| 1026 | |
Christian Gmeiner | 95a428c | 2017-09-24 15:15:20 +0200 | [diff] [blame] | 1027 | static int event_alloc(struct etnaviv_gpu *gpu, unsigned nr_events, |
| 1028 | unsigned int *events) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1029 | { |
Christian Gmeiner | 95a428c | 2017-09-24 15:15:20 +0200 | [diff] [blame] | 1030 | unsigned long flags, timeout = msecs_to_jiffies(10 * 10000); |
| 1031 | unsigned i, acquired = 0; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1032 | |
Christian Gmeiner | 95a428c | 2017-09-24 15:15:20 +0200 | [diff] [blame] | 1033 | for (i = 0; i < nr_events; i++) { |
| 1034 | unsigned long ret; |
| 1035 | |
| 1036 | ret = wait_for_completion_timeout(&gpu->event_free, timeout); |
| 1037 | |
| 1038 | if (!ret) { |
| 1039 | dev_err(gpu->dev, "wait_for_completion_timeout failed"); |
| 1040 | goto out; |
| 1041 | } |
| 1042 | |
| 1043 | acquired++; |
| 1044 | timeout = ret; |
| 1045 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1046 | |
| 1047 | spin_lock_irqsave(&gpu->event_spinlock, flags); |
| 1048 | |
Christian Gmeiner | 95a428c | 2017-09-24 15:15:20 +0200 | [diff] [blame] | 1049 | for (i = 0; i < nr_events; i++) { |
| 1050 | int event = find_first_zero_bit(gpu->event_bitmap, ETNA_NR_EVENTS); |
| 1051 | |
| 1052 | events[i] = event; |
Christian Gmeiner | 547d340 | 2017-09-24 15:15:29 +0200 | [diff] [blame] | 1053 | memset(&gpu->event[event], 0, sizeof(struct etnaviv_event)); |
Christian Gmeiner | 355502e | 2017-09-24 15:15:19 +0200 | [diff] [blame] | 1054 | set_bit(event, gpu->event_bitmap); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | spin_unlock_irqrestore(&gpu->event_spinlock, flags); |
| 1058 | |
Christian Gmeiner | 95a428c | 2017-09-24 15:15:20 +0200 | [diff] [blame] | 1059 | return 0; |
| 1060 | |
| 1061 | out: |
| 1062 | for (i = 0; i < acquired; i++) |
| 1063 | complete(&gpu->event_free); |
| 1064 | |
| 1065 | return -EBUSY; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static void event_free(struct etnaviv_gpu *gpu, unsigned int event) |
| 1069 | { |
| 1070 | unsigned long flags; |
| 1071 | |
| 1072 | spin_lock_irqsave(&gpu->event_spinlock, flags); |
| 1073 | |
Christian Gmeiner | 355502e | 2017-09-24 15:15:19 +0200 | [diff] [blame] | 1074 | if (!test_bit(event, gpu->event_bitmap)) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1075 | dev_warn(gpu->dev, "event %u is already marked as free", |
| 1076 | event); |
| 1077 | spin_unlock_irqrestore(&gpu->event_spinlock, flags); |
| 1078 | } else { |
Christian Gmeiner | 355502e | 2017-09-24 15:15:19 +0200 | [diff] [blame] | 1079 | clear_bit(event, gpu->event_bitmap); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1080 | spin_unlock_irqrestore(&gpu->event_spinlock, flags); |
| 1081 | |
| 1082 | complete(&gpu->event_free); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * Cmdstream submission/retirement: |
| 1088 | */ |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1089 | int etnaviv_gpu_wait_fence_interruptible(struct etnaviv_gpu *gpu, |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1090 | u32 id, struct timespec *timeout) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1091 | { |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1092 | struct dma_fence *fence; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1093 | int ret; |
| 1094 | |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1095 | /* |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1096 | * Look up the fence and take a reference. We might still find a fence |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1097 | * whose refcount has already dropped to zero. dma_fence_get_rcu |
| 1098 | * pretends we didn't find a fence in that case. |
| 1099 | */ |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1100 | rcu_read_lock(); |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1101 | fence = idr_find(&gpu->fence_idr, id); |
| 1102 | if (fence) |
| 1103 | fence = dma_fence_get_rcu(fence); |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1104 | rcu_read_unlock(); |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1105 | |
| 1106 | if (!fence) |
| 1107 | return 0; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1108 | |
| 1109 | if (!timeout) { |
| 1110 | /* No timeout was requested: just test for completion */ |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1111 | ret = dma_fence_is_signaled(fence) ? 0 : -EBUSY; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1112 | } else { |
| 1113 | unsigned long remaining = etnaviv_timeout_to_jiffies(timeout); |
| 1114 | |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1115 | ret = dma_fence_wait_timeout(fence, true, remaining); |
| 1116 | if (ret == 0) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1117 | ret = -ETIMEDOUT; |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1118 | else if (ret != -ERESTARTSYS) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1119 | ret = 0; |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1120 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1121 | } |
| 1122 | |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1123 | dma_fence_put(fence); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1124 | return ret; |
| 1125 | } |
| 1126 | |
| 1127 | /* |
| 1128 | * Wait for an object to become inactive. This, on it's own, is not race |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1129 | * free: the object is moved by the scheduler off the active list, and |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1130 | * then the iova is put. Moreover, the object could be re-submitted just |
| 1131 | * after we notice that it's become inactive. |
| 1132 | * |
| 1133 | * Although the retirement happens under the gpu lock, we don't want to hold |
| 1134 | * that lock in this function while waiting. |
| 1135 | */ |
| 1136 | int etnaviv_gpu_wait_obj_inactive(struct etnaviv_gpu *gpu, |
| 1137 | struct etnaviv_gem_object *etnaviv_obj, struct timespec *timeout) |
| 1138 | { |
| 1139 | unsigned long remaining; |
| 1140 | long ret; |
| 1141 | |
| 1142 | if (!timeout) |
| 1143 | return !is_active(etnaviv_obj) ? 0 : -EBUSY; |
| 1144 | |
| 1145 | remaining = etnaviv_timeout_to_jiffies(timeout); |
| 1146 | |
| 1147 | ret = wait_event_interruptible_timeout(gpu->fence_event, |
| 1148 | !is_active(etnaviv_obj), |
| 1149 | remaining); |
Lucas Stach | fa67ac8 | 2017-11-17 16:35:32 +0100 | [diff] [blame] | 1150 | if (ret > 0) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1151 | return 0; |
Lucas Stach | fa67ac8 | 2017-11-17 16:35:32 +0100 | [diff] [blame] | 1152 | else if (ret == -ERESTARTSYS) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1153 | return -ERESTARTSYS; |
Lucas Stach | fa67ac8 | 2017-11-17 16:35:32 +0100 | [diff] [blame] | 1154 | else |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1155 | return -ETIMEDOUT; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1158 | static void sync_point_perfmon_sample(struct etnaviv_gpu *gpu, |
| 1159 | struct etnaviv_event *event, unsigned int flags) |
| 1160 | { |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1161 | const struct etnaviv_gem_submit *submit = event->submit; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1162 | unsigned int i; |
| 1163 | |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1164 | for (i = 0; i < submit->nr_pmrs; i++) { |
| 1165 | const struct etnaviv_perfmon_request *pmr = submit->pmrs + i; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1166 | |
| 1167 | if (pmr->flags == flags) |
Lucas Stach | 7a9c0fe | 2017-11-24 15:19:16 +0100 | [diff] [blame] | 1168 | etnaviv_perfmon_process(gpu, pmr, submit->exec_state); |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | static void sync_point_perfmon_sample_pre(struct etnaviv_gpu *gpu, |
| 1173 | struct etnaviv_event *event) |
| 1174 | { |
Christian Gmeiner | 2c8b0c5 | 2017-09-24 15:15:39 +0200 | [diff] [blame] | 1175 | u32 val; |
| 1176 | |
| 1177 | /* disable clock gating */ |
| 1178 | val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS); |
| 1179 | val &= ~VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING; |
| 1180 | gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val); |
| 1181 | |
Christian Gmeiner | 04a7d18 | 2017-09-24 15:15:42 +0200 | [diff] [blame] | 1182 | /* enable debug register */ |
| 1183 | val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL); |
| 1184 | val &= ~VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS; |
| 1185 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val); |
| 1186 | |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1187 | sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_PRE); |
| 1188 | } |
| 1189 | |
| 1190 | static void sync_point_perfmon_sample_post(struct etnaviv_gpu *gpu, |
| 1191 | struct etnaviv_event *event) |
| 1192 | { |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1193 | const struct etnaviv_gem_submit *submit = event->submit; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1194 | unsigned int i; |
Christian Gmeiner | 2c8b0c5 | 2017-09-24 15:15:39 +0200 | [diff] [blame] | 1195 | u32 val; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1196 | |
| 1197 | sync_point_perfmon_sample(gpu, event, ETNA_PM_PROCESS_POST); |
| 1198 | |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1199 | for (i = 0; i < submit->nr_pmrs; i++) { |
| 1200 | const struct etnaviv_perfmon_request *pmr = submit->pmrs + i; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1201 | |
| 1202 | *pmr->bo_vma = pmr->sequence; |
| 1203 | } |
Christian Gmeiner | 2c8b0c5 | 2017-09-24 15:15:39 +0200 | [diff] [blame] | 1204 | |
Christian Gmeiner | 04a7d18 | 2017-09-24 15:15:42 +0200 | [diff] [blame] | 1205 | /* disable debug register */ |
| 1206 | val = gpu_read(gpu, VIVS_HI_CLOCK_CONTROL); |
| 1207 | val |= VIVS_HI_CLOCK_CONTROL_DISABLE_DEBUG_REGISTERS; |
| 1208 | gpu_write(gpu, VIVS_HI_CLOCK_CONTROL, val); |
| 1209 | |
Christian Gmeiner | 2c8b0c5 | 2017-09-24 15:15:39 +0200 | [diff] [blame] | 1210 | /* enable clock gating */ |
| 1211 | val = gpu_read(gpu, VIVS_PM_POWER_CONTROLS); |
| 1212 | val |= VIVS_PM_POWER_CONTROLS_ENABLE_MODULE_CLOCK_GATING; |
| 1213 | gpu_write(gpu, VIVS_PM_POWER_CONTROLS, val); |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1217 | /* add bo's to gpu's ring, and kick gpu: */ |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1218 | struct dma_fence *etnaviv_gpu_submit(struct etnaviv_gem_submit *submit) |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1219 | { |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1220 | struct etnaviv_gpu *gpu = submit->gpu; |
| 1221 | struct dma_fence *gpu_fence; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1222 | unsigned int i, nr_events = 1, event[3]; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1223 | int ret; |
| 1224 | |
Lucas Stach | 6d7a20c | 2017-12-06 10:53:27 +0100 | [diff] [blame] | 1225 | if (!submit->runtime_resumed) { |
| 1226 | ret = pm_runtime_get_sync(gpu->dev); |
| 1227 | if (ret < 0) |
| 1228 | return NULL; |
| 1229 | submit->runtime_resumed = true; |
| 1230 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1231 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1232 | /* |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1233 | * if there are performance monitor requests we need to have |
| 1234 | * - a sync point to re-configure gpu and process ETNA_PM_PROCESS_PRE |
| 1235 | * requests. |
| 1236 | * - a sync point to re-configure gpu, process ETNA_PM_PROCESS_POST requests |
| 1237 | * and update the sequence number for userspace. |
| 1238 | */ |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1239 | if (submit->nr_pmrs) |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1240 | nr_events = 3; |
| 1241 | |
| 1242 | ret = event_alloc(gpu, nr_events, event); |
Christian Gmeiner | 95a428c | 2017-09-24 15:15:20 +0200 | [diff] [blame] | 1243 | if (ret) { |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1244 | DRM_ERROR("no free events\n"); |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1245 | return NULL; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1246 | } |
| 1247 | |
Lucas Stach | f3cd1b0 | 2017-03-22 12:07:23 +0100 | [diff] [blame] | 1248 | mutex_lock(&gpu->lock); |
| 1249 | |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1250 | gpu_fence = etnaviv_gpu_fence_alloc(gpu); |
| 1251 | if (!gpu_fence) { |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1252 | for (i = 0; i < nr_events; i++) |
| 1253 | event_free(gpu, event[i]); |
| 1254 | |
Wei Yongjun | 45abdf3 | 2017-04-12 00:31:16 +0000 | [diff] [blame] | 1255 | goto out_unlock; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1256 | } |
| 1257 | |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1258 | gpu->active_fence = gpu_fence->seqno; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1259 | |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1260 | if (submit->nr_pmrs) { |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1261 | gpu->event[event[1]].sync_point = &sync_point_perfmon_sample_pre; |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1262 | kref_get(&submit->refcount); |
| 1263 | gpu->event[event[1]].submit = submit; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1264 | etnaviv_sync_point_queue(gpu, event[1]); |
| 1265 | } |
| 1266 | |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1267 | gpu->event[event[0]].fence = gpu_fence; |
Lucas Stach | 6d7a20c | 2017-12-06 10:53:27 +0100 | [diff] [blame] | 1268 | submit->cmdbuf.user_size = submit->cmdbuf.size - 8; |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 1269 | etnaviv_buffer_queue(gpu, submit->exec_state, event[0], |
| 1270 | &submit->cmdbuf); |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1271 | |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1272 | if (submit->nr_pmrs) { |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1273 | gpu->event[event[2]].sync_point = &sync_point_perfmon_sample_post; |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1274 | kref_get(&submit->refcount); |
| 1275 | gpu->event[event[2]].submit = submit; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1276 | etnaviv_sync_point_queue(gpu, event[2]); |
| 1277 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1278 | |
Wei Yongjun | 45abdf3 | 2017-04-12 00:31:16 +0000 | [diff] [blame] | 1279 | out_unlock: |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1280 | mutex_unlock(&gpu->lock); |
| 1281 | |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1282 | return gpu_fence; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1283 | } |
| 1284 | |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1285 | static void sync_point_worker(struct work_struct *work) |
| 1286 | { |
| 1287 | struct etnaviv_gpu *gpu = container_of(work, struct etnaviv_gpu, |
| 1288 | sync_point_work); |
Lucas Stach | b9a48aa | 2017-10-19 13:48:40 +0200 | [diff] [blame] | 1289 | struct etnaviv_event *event = &gpu->event[gpu->sync_point_event]; |
| 1290 | u32 addr = gpu_read(gpu, VIVS_FE_DMA_ADDRESS); |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1291 | |
Lucas Stach | b9a48aa | 2017-10-19 13:48:40 +0200 | [diff] [blame] | 1292 | event->sync_point(gpu, event); |
Lucas Stach | ef146c00 | 2017-11-24 12:02:38 +0100 | [diff] [blame] | 1293 | etnaviv_submit_put(event->submit); |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1294 | event_free(gpu, gpu->sync_point_event); |
Lucas Stach | b9a48aa | 2017-10-19 13:48:40 +0200 | [diff] [blame] | 1295 | |
| 1296 | /* restart FE last to avoid GPU and IRQ racing against this worker */ |
| 1297 | etnaviv_gpu_start_fe(gpu, addr + 2, 2); |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1298 | } |
| 1299 | |
Lucas Stach | 4df3000 | 2018-01-19 12:22:30 +0100 | [diff] [blame] | 1300 | static void dump_mmu_fault(struct etnaviv_gpu *gpu) |
| 1301 | { |
| 1302 | u32 status = gpu_read(gpu, VIVS_MMUv2_STATUS); |
| 1303 | int i; |
| 1304 | |
| 1305 | dev_err_ratelimited(gpu->dev, "MMU fault status 0x%08x\n", status); |
| 1306 | |
| 1307 | for (i = 0; i < 4; i++) { |
| 1308 | if (!(status & (VIVS_MMUv2_STATUS_EXCEPTION0__MASK << (i * 4)))) |
| 1309 | continue; |
| 1310 | |
| 1311 | dev_err_ratelimited(gpu->dev, "MMU %d fault addr 0x%08x\n", i, |
| 1312 | gpu_read(gpu, VIVS_MMUv2_EXCEPTION_ADDR(i))); |
| 1313 | } |
| 1314 | } |
| 1315 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1316 | static irqreturn_t irq_handler(int irq, void *data) |
| 1317 | { |
| 1318 | struct etnaviv_gpu *gpu = data; |
| 1319 | irqreturn_t ret = IRQ_NONE; |
| 1320 | |
| 1321 | u32 intr = gpu_read(gpu, VIVS_HI_INTR_ACKNOWLEDGE); |
| 1322 | |
| 1323 | if (intr != 0) { |
| 1324 | int event; |
| 1325 | |
| 1326 | pm_runtime_mark_last_busy(gpu->dev); |
| 1327 | |
| 1328 | dev_dbg(gpu->dev, "intr 0x%08x\n", intr); |
| 1329 | |
| 1330 | if (intr & VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR) { |
| 1331 | dev_err(gpu->dev, "AXI bus error\n"); |
| 1332 | intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_AXI_BUS_ERROR; |
| 1333 | } |
| 1334 | |
Lucas Stach | 128a9b1 | 2016-08-20 00:14:43 +0200 | [diff] [blame] | 1335 | if (intr & VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION) { |
Lucas Stach | 4df3000 | 2018-01-19 12:22:30 +0100 | [diff] [blame] | 1336 | dump_mmu_fault(gpu); |
Lucas Stach | 128a9b1 | 2016-08-20 00:14:43 +0200 | [diff] [blame] | 1337 | intr &= ~VIVS_HI_INTR_ACKNOWLEDGE_MMU_EXCEPTION; |
| 1338 | } |
| 1339 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1340 | while ((event = ffs(intr)) != 0) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1341 | struct dma_fence *fence; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1342 | |
| 1343 | event -= 1; |
| 1344 | |
| 1345 | intr &= ~(1 << event); |
| 1346 | |
| 1347 | dev_dbg(gpu->dev, "event %u\n", event); |
| 1348 | |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1349 | if (gpu->event[event].sync_point) { |
| 1350 | gpu->sync_point_event = event; |
Lucas Stach | a7790d7 | 2017-11-17 17:43:37 +0100 | [diff] [blame] | 1351 | queue_work(gpu->wq, &gpu->sync_point_work); |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1352 | } |
| 1353 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1354 | fence = gpu->event[event].fence; |
Christian Gmeiner | 68dc0b2 | 2017-09-24 15:15:30 +0200 | [diff] [blame] | 1355 | if (!fence) |
| 1356 | continue; |
| 1357 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1358 | gpu->event[event].fence = NULL; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1359 | |
| 1360 | /* |
| 1361 | * Events can be processed out of order. Eg, |
| 1362 | * - allocate and queue event 0 |
| 1363 | * - allocate event 1 |
| 1364 | * - event 0 completes, we process it |
| 1365 | * - allocate and queue event 0 |
| 1366 | * - event 1 and event 0 complete |
| 1367 | * we can end up processing event 0 first, then 1. |
| 1368 | */ |
| 1369 | if (fence_after(fence->seqno, gpu->completed_fence)) |
| 1370 | gpu->completed_fence = fence->seqno; |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1371 | dma_fence_signal(fence); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1372 | |
| 1373 | event_free(gpu, event); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1374 | } |
| 1375 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1376 | ret = IRQ_HANDLED; |
| 1377 | } |
| 1378 | |
| 1379 | return ret; |
| 1380 | } |
| 1381 | |
| 1382 | static int etnaviv_gpu_clk_enable(struct etnaviv_gpu *gpu) |
| 1383 | { |
| 1384 | int ret; |
| 1385 | |
Lucas Stach | 65f037e | 2018-01-19 15:05:40 +0100 | [diff] [blame] | 1386 | if (gpu->clk_reg) { |
| 1387 | ret = clk_prepare_enable(gpu->clk_reg); |
| 1388 | if (ret) |
| 1389 | return ret; |
| 1390 | } |
| 1391 | |
Lucas Stach | 9c7310c | 2016-08-22 15:26:19 +0200 | [diff] [blame] | 1392 | if (gpu->clk_bus) { |
| 1393 | ret = clk_prepare_enable(gpu->clk_bus); |
| 1394 | if (ret) |
| 1395 | return ret; |
| 1396 | } |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1397 | |
Lucas Stach | 9c7310c | 2016-08-22 15:26:19 +0200 | [diff] [blame] | 1398 | if (gpu->clk_core) { |
| 1399 | ret = clk_prepare_enable(gpu->clk_core); |
| 1400 | if (ret) |
| 1401 | goto disable_clk_bus; |
| 1402 | } |
| 1403 | |
| 1404 | if (gpu->clk_shader) { |
| 1405 | ret = clk_prepare_enable(gpu->clk_shader); |
| 1406 | if (ret) |
| 1407 | goto disable_clk_core; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | return 0; |
Lucas Stach | 9c7310c | 2016-08-22 15:26:19 +0200 | [diff] [blame] | 1411 | |
| 1412 | disable_clk_core: |
| 1413 | if (gpu->clk_core) |
| 1414 | clk_disable_unprepare(gpu->clk_core); |
| 1415 | disable_clk_bus: |
| 1416 | if (gpu->clk_bus) |
| 1417 | clk_disable_unprepare(gpu->clk_bus); |
| 1418 | |
| 1419 | return ret; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | static int etnaviv_gpu_clk_disable(struct etnaviv_gpu *gpu) |
| 1423 | { |
Lucas Stach | 9c7310c | 2016-08-22 15:26:19 +0200 | [diff] [blame] | 1424 | if (gpu->clk_shader) |
| 1425 | clk_disable_unprepare(gpu->clk_shader); |
| 1426 | if (gpu->clk_core) |
| 1427 | clk_disable_unprepare(gpu->clk_core); |
| 1428 | if (gpu->clk_bus) |
| 1429 | clk_disable_unprepare(gpu->clk_bus); |
Lucas Stach | 65f037e | 2018-01-19 15:05:40 +0100 | [diff] [blame] | 1430 | if (gpu->clk_reg) |
| 1431 | clk_disable_unprepare(gpu->clk_reg); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
Lucas Stach | b88163e | 2016-08-17 15:16:57 +0200 | [diff] [blame] | 1436 | int etnaviv_gpu_wait_idle(struct etnaviv_gpu *gpu, unsigned int timeout_ms) |
| 1437 | { |
| 1438 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 1439 | |
| 1440 | do { |
| 1441 | u32 idle = gpu_read(gpu, VIVS_HI_IDLE_STATE); |
| 1442 | |
| 1443 | if ((idle & gpu->idle_mask) == gpu->idle_mask) |
| 1444 | return 0; |
| 1445 | |
| 1446 | if (time_is_before_jiffies(timeout)) { |
| 1447 | dev_warn(gpu->dev, |
| 1448 | "timed out waiting for idle: idle=0x%x\n", |
| 1449 | idle); |
| 1450 | return -ETIMEDOUT; |
| 1451 | } |
| 1452 | |
| 1453 | udelay(5); |
| 1454 | } while (1); |
| 1455 | } |
| 1456 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1457 | static int etnaviv_gpu_hw_suspend(struct etnaviv_gpu *gpu) |
| 1458 | { |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 1459 | if (gpu->buffer.suballoc) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1460 | /* Replace the last WAIT with END */ |
Lucas Stach | 40c27bd | 2017-11-17 17:59:26 +0100 | [diff] [blame] | 1461 | mutex_lock(&gpu->lock); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1462 | etnaviv_buffer_end(gpu); |
Lucas Stach | 40c27bd | 2017-11-17 17:59:26 +0100 | [diff] [blame] | 1463 | mutex_unlock(&gpu->lock); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1464 | |
| 1465 | /* |
| 1466 | * We know that only the FE is busy here, this should |
| 1467 | * happen quickly (as the WAIT is only 200 cycles). If |
| 1468 | * we fail, just warn and continue. |
| 1469 | */ |
Lucas Stach | b88163e | 2016-08-17 15:16:57 +0200 | [diff] [blame] | 1470 | etnaviv_gpu_wait_idle(gpu, 100); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | return etnaviv_gpu_clk_disable(gpu); |
| 1474 | } |
| 1475 | |
| 1476 | #ifdef CONFIG_PM |
| 1477 | static int etnaviv_gpu_hw_resume(struct etnaviv_gpu *gpu) |
| 1478 | { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1479 | int ret; |
| 1480 | |
| 1481 | ret = mutex_lock_killable(&gpu->lock); |
| 1482 | if (ret) |
| 1483 | return ret; |
| 1484 | |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 1485 | etnaviv_gpu_update_clock(gpu); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1486 | etnaviv_gpu_hw_init(gpu); |
| 1487 | |
Lucas Stach | 4375fff | 2017-11-17 17:19:50 +0100 | [diff] [blame] | 1488 | gpu->lastctx = NULL; |
Russell King | f608631 | 2016-01-21 15:20:19 +0000 | [diff] [blame] | 1489 | gpu->exec_state = -1; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1490 | |
| 1491 | mutex_unlock(&gpu->lock); |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | #endif |
| 1496 | |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 1497 | static int |
| 1498 | etnaviv_gpu_cooling_get_max_state(struct thermal_cooling_device *cdev, |
| 1499 | unsigned long *state) |
| 1500 | { |
| 1501 | *state = 6; |
| 1502 | |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
| 1506 | static int |
| 1507 | etnaviv_gpu_cooling_get_cur_state(struct thermal_cooling_device *cdev, |
| 1508 | unsigned long *state) |
| 1509 | { |
| 1510 | struct etnaviv_gpu *gpu = cdev->devdata; |
| 1511 | |
| 1512 | *state = gpu->freq_scale; |
| 1513 | |
| 1514 | return 0; |
| 1515 | } |
| 1516 | |
| 1517 | static int |
| 1518 | etnaviv_gpu_cooling_set_cur_state(struct thermal_cooling_device *cdev, |
| 1519 | unsigned long state) |
| 1520 | { |
| 1521 | struct etnaviv_gpu *gpu = cdev->devdata; |
| 1522 | |
| 1523 | mutex_lock(&gpu->lock); |
| 1524 | gpu->freq_scale = state; |
| 1525 | if (!pm_runtime_suspended(gpu->dev)) |
| 1526 | etnaviv_gpu_update_clock(gpu); |
| 1527 | mutex_unlock(&gpu->lock); |
| 1528 | |
| 1529 | return 0; |
| 1530 | } |
| 1531 | |
| 1532 | static struct thermal_cooling_device_ops cooling_ops = { |
| 1533 | .get_max_state = etnaviv_gpu_cooling_get_max_state, |
| 1534 | .get_cur_state = etnaviv_gpu_cooling_get_cur_state, |
| 1535 | .set_cur_state = etnaviv_gpu_cooling_set_cur_state, |
| 1536 | }; |
| 1537 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1538 | static int etnaviv_gpu_bind(struct device *dev, struct device *master, |
| 1539 | void *data) |
| 1540 | { |
| 1541 | struct drm_device *drm = data; |
| 1542 | struct etnaviv_drm_private *priv = drm->dev_private; |
| 1543 | struct etnaviv_gpu *gpu = dev_get_drvdata(dev); |
| 1544 | int ret; |
| 1545 | |
Philipp Zabel | 49b82c3 | 2017-12-01 16:00:41 +0100 | [diff] [blame] | 1546 | if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) { |
Lucas Stach | 5247e2a | 2017-08-08 15:28:25 +0200 | [diff] [blame] | 1547 | gpu->cooling = thermal_of_cooling_device_register(dev->of_node, |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 1548 | (char *)dev_name(dev), gpu, &cooling_ops); |
Lucas Stach | 5247e2a | 2017-08-08 15:28:25 +0200 | [diff] [blame] | 1549 | if (IS_ERR(gpu->cooling)) |
| 1550 | return PTR_ERR(gpu->cooling); |
| 1551 | } |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 1552 | |
Lucas Stach | a7790d7 | 2017-11-17 17:43:37 +0100 | [diff] [blame] | 1553 | gpu->wq = alloc_ordered_workqueue(dev_name(dev), 0); |
| 1554 | if (!gpu->wq) { |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1555 | ret = -ENOMEM; |
| 1556 | goto out_thermal; |
Lucas Stach | a7790d7 | 2017-11-17 17:43:37 +0100 | [diff] [blame] | 1557 | } |
| 1558 | |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1559 | ret = etnaviv_sched_init(gpu); |
| 1560 | if (ret) |
| 1561 | goto out_workqueue; |
| 1562 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1563 | #ifdef CONFIG_PM |
| 1564 | ret = pm_runtime_get_sync(gpu->dev); |
| 1565 | #else |
| 1566 | ret = etnaviv_gpu_clk_enable(gpu); |
| 1567 | #endif |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1568 | if (ret < 0) |
| 1569 | goto out_sched; |
| 1570 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1571 | |
| 1572 | gpu->drm = drm; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1573 | gpu->fence_context = dma_fence_context_alloc(1); |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1574 | idr_init(&gpu->fence_idr); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1575 | spin_lock_init(&gpu->fence_spinlock); |
| 1576 | |
Christian Gmeiner | 357713c | 2017-09-24 15:15:28 +0200 | [diff] [blame] | 1577 | INIT_WORK(&gpu->sync_point_work, sync_point_worker); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1578 | init_waitqueue_head(&gpu->fence_event); |
| 1579 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1580 | priv->gpu[priv->num_gpus++] = gpu; |
| 1581 | |
| 1582 | pm_runtime_mark_last_busy(gpu->dev); |
| 1583 | pm_runtime_put_autosuspend(gpu->dev); |
| 1584 | |
| 1585 | return 0; |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1586 | |
| 1587 | out_sched: |
| 1588 | etnaviv_sched_fini(gpu); |
| 1589 | |
| 1590 | out_workqueue: |
| 1591 | destroy_workqueue(gpu->wq); |
| 1592 | |
| 1593 | out_thermal: |
| 1594 | if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) |
| 1595 | thermal_cooling_device_unregister(gpu->cooling); |
| 1596 | |
| 1597 | return ret; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | static void etnaviv_gpu_unbind(struct device *dev, struct device *master, |
| 1601 | void *data) |
| 1602 | { |
| 1603 | struct etnaviv_gpu *gpu = dev_get_drvdata(dev); |
| 1604 | |
| 1605 | DBG("%s", dev_name(gpu->dev)); |
| 1606 | |
Lucas Stach | a7790d7 | 2017-11-17 17:43:37 +0100 | [diff] [blame] | 1607 | flush_workqueue(gpu->wq); |
| 1608 | destroy_workqueue(gpu->wq); |
| 1609 | |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1610 | etnaviv_sched_fini(gpu); |
| 1611 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1612 | #ifdef CONFIG_PM |
| 1613 | pm_runtime_get_sync(gpu->dev); |
| 1614 | pm_runtime_put_sync_suspend(gpu->dev); |
| 1615 | #else |
| 1616 | etnaviv_gpu_hw_suspend(gpu); |
| 1617 | #endif |
| 1618 | |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 1619 | if (gpu->buffer.suballoc) |
| 1620 | etnaviv_cmdbuf_free(&gpu->buffer); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1621 | |
Lucas Stach | e66774d | 2017-01-16 17:29:57 +0100 | [diff] [blame] | 1622 | if (gpu->cmdbuf_suballoc) { |
| 1623 | etnaviv_cmdbuf_suballoc_destroy(gpu->cmdbuf_suballoc); |
| 1624 | gpu->cmdbuf_suballoc = NULL; |
| 1625 | } |
| 1626 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1627 | if (gpu->mmu) { |
| 1628 | etnaviv_iommu_destroy(gpu->mmu); |
| 1629 | gpu->mmu = NULL; |
| 1630 | } |
| 1631 | |
| 1632 | gpu->drm = NULL; |
Lucas Stach | 8bc4d88 | 2017-11-29 14:49:04 +0100 | [diff] [blame] | 1633 | idr_destroy(&gpu->fence_idr); |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 1634 | |
Philipp Zabel | 49b82c3 | 2017-12-01 16:00:41 +0100 | [diff] [blame] | 1635 | if (IS_ENABLED(CONFIG_DRM_ETNAVIV_THERMAL)) |
| 1636 | thermal_cooling_device_unregister(gpu->cooling); |
Russell King | bcdfb5e | 2017-03-12 19:00:59 +0000 | [diff] [blame] | 1637 | gpu->cooling = NULL; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | static const struct component_ops gpu_ops = { |
| 1641 | .bind = etnaviv_gpu_bind, |
| 1642 | .unbind = etnaviv_gpu_unbind, |
| 1643 | }; |
| 1644 | |
| 1645 | static const struct of_device_id etnaviv_gpu_match[] = { |
| 1646 | { |
| 1647 | .compatible = "vivante,gc" |
| 1648 | }, |
| 1649 | { /* sentinel */ } |
| 1650 | }; |
Lucas Stach | 246774d | 2018-01-24 15:30:29 +0100 | [diff] [blame] | 1651 | MODULE_DEVICE_TABLE(of, etnaviv_gpu_match); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1652 | |
| 1653 | static int etnaviv_gpu_platform_probe(struct platform_device *pdev) |
| 1654 | { |
| 1655 | struct device *dev = &pdev->dev; |
| 1656 | struct etnaviv_gpu *gpu; |
Fabio Estevam | dc22789 | 2016-08-21 19:32:15 -0300 | [diff] [blame] | 1657 | int err; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1658 | |
| 1659 | gpu = devm_kzalloc(dev, sizeof(*gpu), GFP_KERNEL); |
| 1660 | if (!gpu) |
| 1661 | return -ENOMEM; |
| 1662 | |
| 1663 | gpu->dev = &pdev->dev; |
| 1664 | mutex_init(&gpu->lock); |
Lucas Stach | e93b6de | 2017-12-04 18:41:58 +0100 | [diff] [blame] | 1665 | mutex_init(&gpu->fence_idr_lock); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1666 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1667 | /* Map registers: */ |
| 1668 | gpu->mmio = etnaviv_ioremap(pdev, NULL, dev_name(gpu->dev)); |
| 1669 | if (IS_ERR(gpu->mmio)) |
| 1670 | return PTR_ERR(gpu->mmio); |
| 1671 | |
| 1672 | /* Get Interrupt: */ |
| 1673 | gpu->irq = platform_get_irq(pdev, 0); |
| 1674 | if (gpu->irq < 0) { |
Fabio Estevam | db60eda | 2016-08-21 19:32:14 -0300 | [diff] [blame] | 1675 | dev_err(dev, "failed to get irq: %d\n", gpu->irq); |
| 1676 | return gpu->irq; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | err = devm_request_irq(&pdev->dev, gpu->irq, irq_handler, 0, |
| 1680 | dev_name(gpu->dev), gpu); |
| 1681 | if (err) { |
| 1682 | dev_err(dev, "failed to request IRQ%u: %d\n", gpu->irq, err); |
Fabio Estevam | db60eda | 2016-08-21 19:32:14 -0300 | [diff] [blame] | 1683 | return err; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1684 | } |
| 1685 | |
| 1686 | /* Get Clocks: */ |
Lucas Stach | 65f037e | 2018-01-19 15:05:40 +0100 | [diff] [blame] | 1687 | gpu->clk_reg = devm_clk_get(&pdev->dev, "reg"); |
| 1688 | DBG("clk_reg: %p", gpu->clk_reg); |
| 1689 | if (IS_ERR(gpu->clk_reg)) |
| 1690 | gpu->clk_reg = NULL; |
| 1691 | |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1692 | gpu->clk_bus = devm_clk_get(&pdev->dev, "bus"); |
| 1693 | DBG("clk_bus: %p", gpu->clk_bus); |
| 1694 | if (IS_ERR(gpu->clk_bus)) |
| 1695 | gpu->clk_bus = NULL; |
| 1696 | |
| 1697 | gpu->clk_core = devm_clk_get(&pdev->dev, "core"); |
| 1698 | DBG("clk_core: %p", gpu->clk_core); |
| 1699 | if (IS_ERR(gpu->clk_core)) |
| 1700 | gpu->clk_core = NULL; |
Lucas Stach | d79fd1ccf2 | 2017-04-11 15:54:50 +0200 | [diff] [blame] | 1701 | gpu->base_rate_core = clk_get_rate(gpu->clk_core); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1702 | |
| 1703 | gpu->clk_shader = devm_clk_get(&pdev->dev, "shader"); |
| 1704 | DBG("clk_shader: %p", gpu->clk_shader); |
| 1705 | if (IS_ERR(gpu->clk_shader)) |
| 1706 | gpu->clk_shader = NULL; |
Lucas Stach | d79fd1ccf2 | 2017-04-11 15:54:50 +0200 | [diff] [blame] | 1707 | gpu->base_rate_shader = clk_get_rate(gpu->clk_shader); |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1708 | |
| 1709 | /* TODO: figure out max mapped size */ |
| 1710 | dev_set_drvdata(dev, gpu); |
| 1711 | |
| 1712 | /* |
| 1713 | * We treat the device as initially suspended. The runtime PM |
| 1714 | * autosuspend delay is rather arbitary: no measurements have |
| 1715 | * yet been performed to determine an appropriate value. |
| 1716 | */ |
| 1717 | pm_runtime_use_autosuspend(gpu->dev); |
| 1718 | pm_runtime_set_autosuspend_delay(gpu->dev, 200); |
| 1719 | pm_runtime_enable(gpu->dev); |
| 1720 | |
| 1721 | err = component_add(&pdev->dev, &gpu_ops); |
| 1722 | if (err < 0) { |
| 1723 | dev_err(&pdev->dev, "failed to register component: %d\n", err); |
Fabio Estevam | db60eda | 2016-08-21 19:32:14 -0300 | [diff] [blame] | 1724 | return err; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | return 0; |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static int etnaviv_gpu_platform_remove(struct platform_device *pdev) |
| 1731 | { |
| 1732 | component_del(&pdev->dev, &gpu_ops); |
| 1733 | pm_runtime_disable(&pdev->dev); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | #ifdef CONFIG_PM |
| 1738 | static int etnaviv_gpu_rpm_suspend(struct device *dev) |
| 1739 | { |
| 1740 | struct etnaviv_gpu *gpu = dev_get_drvdata(dev); |
| 1741 | u32 idle, mask; |
| 1742 | |
| 1743 | /* If we have outstanding fences, we're not idle */ |
| 1744 | if (gpu->completed_fence != gpu->active_fence) |
| 1745 | return -EBUSY; |
| 1746 | |
| 1747 | /* Check whether the hardware (except FE) is idle */ |
| 1748 | mask = gpu->idle_mask & ~VIVS_HI_IDLE_STATE_FE; |
| 1749 | idle = gpu_read(gpu, VIVS_HI_IDLE_STATE) & mask; |
| 1750 | if (idle != mask) |
| 1751 | return -EBUSY; |
| 1752 | |
| 1753 | return etnaviv_gpu_hw_suspend(gpu); |
| 1754 | } |
| 1755 | |
| 1756 | static int etnaviv_gpu_rpm_resume(struct device *dev) |
| 1757 | { |
| 1758 | struct etnaviv_gpu *gpu = dev_get_drvdata(dev); |
| 1759 | int ret; |
| 1760 | |
| 1761 | ret = etnaviv_gpu_clk_enable(gpu); |
| 1762 | if (ret) |
| 1763 | return ret; |
| 1764 | |
| 1765 | /* Re-initialise the basic hardware state */ |
Lucas Stach | 2f9225d | 2017-11-24 16:56:37 +0100 | [diff] [blame] | 1766 | if (gpu->drm && gpu->buffer.suballoc) { |
The etnaviv authors | a8c21a5 | 2015-12-03 18:21:29 +0100 | [diff] [blame] | 1767 | ret = etnaviv_gpu_hw_resume(gpu); |
| 1768 | if (ret) { |
| 1769 | etnaviv_gpu_clk_disable(gpu); |
| 1770 | return ret; |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | #endif |
| 1777 | |
| 1778 | static const struct dev_pm_ops etnaviv_gpu_pm_ops = { |
| 1779 | SET_RUNTIME_PM_OPS(etnaviv_gpu_rpm_suspend, etnaviv_gpu_rpm_resume, |
| 1780 | NULL) |
| 1781 | }; |
| 1782 | |
| 1783 | struct platform_driver etnaviv_gpu_driver = { |
| 1784 | .driver = { |
| 1785 | .name = "etnaviv-gpu", |
| 1786 | .owner = THIS_MODULE, |
| 1787 | .pm = &etnaviv_gpu_pm_ops, |
| 1788 | .of_match_table = etnaviv_gpu_match, |
| 1789 | }, |
| 1790 | .probe = etnaviv_gpu_platform_probe, |
| 1791 | .remove = etnaviv_gpu_platform_remove, |
| 1792 | .id_table = gpu_ids, |
| 1793 | }; |