1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */ 3 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */ 4 5 #include <linux/clk.h> 6 #include <linux/reset.h> 7 #include <linux/platform_device.h> 8 #include <linux/pm_domain.h> 9 #include <linux/regulator/consumer.h> 10 11 #include "panfrost_device.h" 12 #include "panfrost_devfreq.h" 13 #include "panfrost_features.h" 14 #include "panfrost_gpu.h" 15 #include "panfrost_job.h" 16 #include "panfrost_mmu.h" 17 #include "panfrost_perfcnt.h" 18 19 static int panfrost_reset_init(struct panfrost_device *pfdev) 20 { 21 int err; 22 23 pfdev->rstc = devm_reset_control_array_get(pfdev->dev, false, true); 24 if (IS_ERR(pfdev->rstc)) { 25 dev_err(pfdev->dev, "get reset failed %ld\n", PTR_ERR(pfdev->rstc)); 26 return PTR_ERR(pfdev->rstc); 27 } 28 29 err = reset_control_deassert(pfdev->rstc); 30 if (err) 31 return err; 32 33 return 0; 34 } 35 36 static void panfrost_reset_fini(struct panfrost_device *pfdev) 37 { 38 reset_control_assert(pfdev->rstc); 39 } 40 41 static int panfrost_clk_init(struct panfrost_device *pfdev) 42 { 43 int err; 44 unsigned long rate; 45 46 pfdev->clock = devm_clk_get(pfdev->dev, NULL); 47 if (IS_ERR(pfdev->clock)) { 48 dev_err(pfdev->dev, "get clock failed %ld\n", PTR_ERR(pfdev->clock)); 49 return PTR_ERR(pfdev->clock); 50 } 51 52 rate = clk_get_rate(pfdev->clock); 53 dev_info(pfdev->dev, "clock rate = %lu\n", rate); 54 55 err = clk_prepare_enable(pfdev->clock); 56 if (err) 57 return err; 58 59 pfdev->bus_clock = devm_clk_get_optional(pfdev->dev, "bus"); 60 if (IS_ERR(pfdev->bus_clock)) { 61 dev_err(pfdev->dev, "get bus_clock failed %ld\n", 62 PTR_ERR(pfdev->bus_clock)); 63 return PTR_ERR(pfdev->bus_clock); 64 } 65 66 if (pfdev->bus_clock) { 67 rate = clk_get_rate(pfdev->bus_clock); 68 dev_info(pfdev->dev, "bus_clock rate = %lu\n", rate); 69 70 err = clk_prepare_enable(pfdev->bus_clock); 71 if (err) 72 goto disable_clock; 73 } 74 75 return 0; 76 77 disable_clock: 78 clk_disable_unprepare(pfdev->clock); 79 80 return err; 81 } 82 83 static void panfrost_clk_fini(struct panfrost_device *pfdev) 84 { 85 clk_disable_unprepare(pfdev->bus_clock); 86 clk_disable_unprepare(pfdev->clock); 87 } 88 89 static int panfrost_regulator_init(struct panfrost_device *pfdev) 90 { 91 int ret, i; 92 93 pfdev->regulators = devm_kcalloc(pfdev->dev, pfdev->comp->num_supplies, 94 sizeof(*pfdev->regulators), 95 GFP_KERNEL); 96 if (!pfdev->regulators) 97 return -ENOMEM; 98 99 for (i = 0; i < pfdev->comp->num_supplies; i++) 100 pfdev->regulators[i].supply = pfdev->comp->supply_names[i]; 101 102 ret = devm_regulator_bulk_get(pfdev->dev, 103 pfdev->comp->num_supplies, 104 pfdev->regulators); 105 if (ret < 0) { 106 if (ret != -EPROBE_DEFER) 107 dev_err(pfdev->dev, "failed to get regulators: %d\n", 108 ret); 109 return ret; 110 } 111 112 ret = regulator_bulk_enable(pfdev->comp->num_supplies, 113 pfdev->regulators); 114 if (ret < 0) { 115 dev_err(pfdev->dev, "failed to enable regulators: %d\n", ret); 116 return ret; 117 } 118 119 return 0; 120 } 121 122 static void panfrost_regulator_fini(struct panfrost_device *pfdev) 123 { 124 if (!pfdev->regulators) 125 return; 126 127 regulator_bulk_disable(pfdev->comp->num_supplies, pfdev->regulators); 128 } 129 130 static void panfrost_pm_domain_fini(struct panfrost_device *pfdev) 131 { 132 int i; 133 134 for (i = 0; i < ARRAY_SIZE(pfdev->pm_domain_devs); i++) { 135 if (!pfdev->pm_domain_devs[i]) 136 break; 137 138 if (pfdev->pm_domain_links[i]) 139 device_link_del(pfdev->pm_domain_links[i]); 140 141 dev_pm_domain_detach(pfdev->pm_domain_devs[i], true); 142 } 143 } 144 145 static int panfrost_pm_domain_init(struct panfrost_device *pfdev) 146 { 147 int err; 148 int i, num_domains; 149 150 num_domains = of_count_phandle_with_args(pfdev->dev->of_node, 151 "power-domains", 152 "#power-domain-cells"); 153 154 /* 155 * Single domain is handled by the core, and, if only a single power 156 * the power domain is requested, the property is optional. 157 */ 158 if (num_domains < 2 && pfdev->comp->num_pm_domains < 2) 159 return 0; 160 161 if (num_domains != pfdev->comp->num_pm_domains) { 162 dev_err(pfdev->dev, 163 "Incorrect number of power domains: %d provided, %d needed\n", 164 num_domains, pfdev->comp->num_pm_domains); 165 return -EINVAL; 166 } 167 168 if (WARN(num_domains > ARRAY_SIZE(pfdev->pm_domain_devs), 169 "Too many supplies in compatible structure.\n")) 170 return -EINVAL; 171 172 for (i = 0; i < num_domains; i++) { 173 pfdev->pm_domain_devs[i] = 174 dev_pm_domain_attach_by_name(pfdev->dev, 175 pfdev->comp->pm_domain_names[i]); 176 if (IS_ERR_OR_NULL(pfdev->pm_domain_devs[i])) { 177 err = PTR_ERR(pfdev->pm_domain_devs[i]) ? : -ENODATA; 178 pfdev->pm_domain_devs[i] = NULL; 179 dev_err(pfdev->dev, 180 "failed to get pm-domain %s(%d): %d\n", 181 pfdev->comp->pm_domain_names[i], i, err); 182 goto err; 183 } 184 185 pfdev->pm_domain_links[i] = device_link_add(pfdev->dev, 186 pfdev->pm_domain_devs[i], DL_FLAG_PM_RUNTIME | 187 DL_FLAG_STATELESS | DL_FLAG_RPM_ACTIVE); 188 if (!pfdev->pm_domain_links[i]) { 189 dev_err(pfdev->pm_domain_devs[i], 190 "adding device link failed!\n"); 191 err = -ENODEV; 192 goto err; 193 } 194 } 195 196 return 0; 197 198 err: 199 panfrost_pm_domain_fini(pfdev); 200 return err; 201 } 202 203 int panfrost_device_init(struct panfrost_device *pfdev) 204 { 205 int err; 206 struct resource *res; 207 208 mutex_init(&pfdev->sched_lock); 209 mutex_init(&pfdev->reset_lock); 210 INIT_LIST_HEAD(&pfdev->scheduled_jobs); 211 INIT_LIST_HEAD(&pfdev->as_lru_list); 212 213 spin_lock_init(&pfdev->as_lock); 214 215 err = panfrost_clk_init(pfdev); 216 if (err) { 217 dev_err(pfdev->dev, "clk init failed %d\n", err); 218 return err; 219 } 220 221 err = panfrost_devfreq_init(pfdev); 222 if (err) { 223 if (err != -EPROBE_DEFER) 224 dev_err(pfdev->dev, "devfreq init failed %d\n", err); 225 goto out_clk; 226 } 227 228 /* OPP will handle regulators */ 229 if (!pfdev->pfdevfreq.opp_of_table_added) { 230 err = panfrost_regulator_init(pfdev); 231 if (err) 232 goto out_devfreq; 233 } 234 235 err = panfrost_reset_init(pfdev); 236 if (err) { 237 dev_err(pfdev->dev, "reset init failed %d\n", err); 238 goto out_regulator; 239 } 240 241 err = panfrost_pm_domain_init(pfdev); 242 if (err) 243 goto out_reset; 244 245 res = platform_get_resource(pfdev->pdev, IORESOURCE_MEM, 0); 246 pfdev->iomem = devm_ioremap_resource(pfdev->dev, res); 247 if (IS_ERR(pfdev->iomem)) { 248 dev_err(pfdev->dev, "failed to ioremap iomem\n"); 249 err = PTR_ERR(pfdev->iomem); 250 goto out_pm_domain; 251 } 252 253 err = panfrost_gpu_init(pfdev); 254 if (err) 255 goto out_pm_domain; 256 257 err = panfrost_mmu_init(pfdev); 258 if (err) 259 goto out_gpu; 260 261 err = panfrost_job_init(pfdev); 262 if (err) 263 goto out_mmu; 264 265 err = panfrost_perfcnt_init(pfdev); 266 if (err) 267 goto out_job; 268 269 return 0; 270 out_job: 271 panfrost_job_fini(pfdev); 272 out_mmu: 273 panfrost_mmu_fini(pfdev); 274 out_gpu: 275 panfrost_gpu_fini(pfdev); 276 out_pm_domain: 277 panfrost_pm_domain_fini(pfdev); 278 out_reset: 279 panfrost_reset_fini(pfdev); 280 out_regulator: 281 panfrost_regulator_fini(pfdev); 282 out_devfreq: 283 panfrost_devfreq_fini(pfdev); 284 out_clk: 285 panfrost_clk_fini(pfdev); 286 return err; 287 } 288 289 void panfrost_device_fini(struct panfrost_device *pfdev) 290 { 291 panfrost_perfcnt_fini(pfdev); 292 panfrost_job_fini(pfdev); 293 panfrost_mmu_fini(pfdev); 294 panfrost_gpu_fini(pfdev); 295 panfrost_pm_domain_fini(pfdev); 296 panfrost_reset_fini(pfdev); 297 panfrost_devfreq_fini(pfdev); 298 panfrost_regulator_fini(pfdev); 299 panfrost_clk_fini(pfdev); 300 } 301 302 const char *panfrost_exception_name(struct panfrost_device *pfdev, u32 exception_code) 303 { 304 switch (exception_code) { 305 /* Non-Fault Status code */ 306 case 0x00: return "NOT_STARTED/IDLE/OK"; 307 case 0x01: return "DONE"; 308 case 0x02: return "INTERRUPTED"; 309 case 0x03: return "STOPPED"; 310 case 0x04: return "TERMINATED"; 311 case 0x08: return "ACTIVE"; 312 /* Job exceptions */ 313 case 0x40: return "JOB_CONFIG_FAULT"; 314 case 0x41: return "JOB_POWER_FAULT"; 315 case 0x42: return "JOB_READ_FAULT"; 316 case 0x43: return "JOB_WRITE_FAULT"; 317 case 0x44: return "JOB_AFFINITY_FAULT"; 318 case 0x48: return "JOB_BUS_FAULT"; 319 case 0x50: return "INSTR_INVALID_PC"; 320 case 0x51: return "INSTR_INVALID_ENC"; 321 case 0x52: return "INSTR_TYPE_MISMATCH"; 322 case 0x53: return "INSTR_OPERAND_FAULT"; 323 case 0x54: return "INSTR_TLS_FAULT"; 324 case 0x55: return "INSTR_BARRIER_FAULT"; 325 case 0x56: return "INSTR_ALIGN_FAULT"; 326 case 0x58: return "DATA_INVALID_FAULT"; 327 case 0x59: return "TILE_RANGE_FAULT"; 328 case 0x5A: return "ADDR_RANGE_FAULT"; 329 case 0x60: return "OUT_OF_MEMORY"; 330 /* GPU exceptions */ 331 case 0x80: return "DELAYED_BUS_FAULT"; 332 case 0x88: return "SHAREABILITY_FAULT"; 333 /* MMU exceptions */ 334 case 0xC1: return "TRANSLATION_FAULT_LEVEL1"; 335 case 0xC2: return "TRANSLATION_FAULT_LEVEL2"; 336 case 0xC3: return "TRANSLATION_FAULT_LEVEL3"; 337 case 0xC4: return "TRANSLATION_FAULT_LEVEL4"; 338 case 0xC8: return "PERMISSION_FAULT"; 339 case 0xC9 ... 0xCF: return "PERMISSION_FAULT"; 340 case 0xD1: return "TRANSTAB_BUS_FAULT_LEVEL1"; 341 case 0xD2: return "TRANSTAB_BUS_FAULT_LEVEL2"; 342 case 0xD3: return "TRANSTAB_BUS_FAULT_LEVEL3"; 343 case 0xD4: return "TRANSTAB_BUS_FAULT_LEVEL4"; 344 case 0xD8: return "ACCESS_FLAG"; 345 case 0xD9 ... 0xDF: return "ACCESS_FLAG"; 346 case 0xE0 ... 0xE7: return "ADDRESS_SIZE_FAULT"; 347 case 0xE8 ... 0xEF: return "MEMORY_ATTRIBUTES_FAULT"; 348 } 349 350 return "UNKNOWN"; 351 } 352 353 void panfrost_device_reset(struct panfrost_device *pfdev) 354 { 355 panfrost_gpu_soft_reset(pfdev); 356 357 panfrost_gpu_power_on(pfdev); 358 panfrost_mmu_reset(pfdev); 359 panfrost_job_enable_interrupts(pfdev); 360 } 361 362 #ifdef CONFIG_PM 363 int panfrost_device_resume(struct device *dev) 364 { 365 struct platform_device *pdev = to_platform_device(dev); 366 struct panfrost_device *pfdev = platform_get_drvdata(pdev); 367 368 panfrost_device_reset(pfdev); 369 panfrost_devfreq_resume(pfdev); 370 371 return 0; 372 } 373 374 int panfrost_device_suspend(struct device *dev) 375 { 376 struct platform_device *pdev = to_platform_device(dev); 377 struct panfrost_device *pfdev = platform_get_drvdata(pdev); 378 379 if (!panfrost_job_is_idle(pfdev)) 380 return -EBUSY; 381 382 panfrost_devfreq_suspend(pfdev); 383 panfrost_gpu_power_off(pfdev); 384 385 return 0; 386 } 387 #endif 388