1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013-2014 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
6  * Copyright (c) 2014,2017 The Linux Foundation. All rights reserved.
7  */
8 
9 #include "adreno_gpu.h"
10 
11 #define ANY_ID 0xff
12 
13 bool hang_debug = false;
14 MODULE_PARM_DESC(hang_debug, "Dump registers when hang is detected (can be slow!)");
15 module_param_named(hang_debug, hang_debug, bool, 0600);
16 
17 static const struct adreno_info gpulist[] = {
18 	{
19 		.rev   = ADRENO_REV(2, 0, 0, 0),
20 		.revn  = 200,
21 		.name  = "A200",
22 		.fw = {
23 			[ADRENO_FW_PM4] = "yamato_pm4.fw",
24 			[ADRENO_FW_PFP] = "yamato_pfp.fw",
25 		},
26 		.gmem  = SZ_256K,
27 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
28 		.init  = a2xx_gpu_init,
29 	}, { /* a200 on i.mx51 has only 128kib gmem */
30 		.rev   = ADRENO_REV(2, 0, 0, 1),
31 		.revn  = 201,
32 		.name  = "A200",
33 		.fw = {
34 			[ADRENO_FW_PM4] = "yamato_pm4.fw",
35 			[ADRENO_FW_PFP] = "yamato_pfp.fw",
36 		},
37 		.gmem  = SZ_128K,
38 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
39 		.init  = a2xx_gpu_init,
40 	}, {
41 		.rev   = ADRENO_REV(2, 2, 0, ANY_ID),
42 		.revn  = 220,
43 		.name  = "A220",
44 		.fw = {
45 			[ADRENO_FW_PM4] = "leia_pm4_470.fw",
46 			[ADRENO_FW_PFP] = "leia_pfp_470.fw",
47 		},
48 		.gmem  = SZ_512K,
49 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
50 		.init  = a2xx_gpu_init,
51 	}, {
52 		.rev   = ADRENO_REV(3, 0, 5, ANY_ID),
53 		.revn  = 305,
54 		.name  = "A305",
55 		.fw = {
56 			[ADRENO_FW_PM4] = "a300_pm4.fw",
57 			[ADRENO_FW_PFP] = "a300_pfp.fw",
58 		},
59 		.gmem  = SZ_256K,
60 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
61 		.init  = a3xx_gpu_init,
62 	}, {
63 		.rev   = ADRENO_REV(3, 0, 6, 0),
64 		.revn  = 307,        /* because a305c is revn==306 */
65 		.name  = "A306",
66 		.fw = {
67 			[ADRENO_FW_PM4] = "a300_pm4.fw",
68 			[ADRENO_FW_PFP] = "a300_pfp.fw",
69 		},
70 		.gmem  = SZ_128K,
71 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
72 		.init  = a3xx_gpu_init,
73 	}, {
74 		.rev   = ADRENO_REV(3, 2, ANY_ID, ANY_ID),
75 		.revn  = 320,
76 		.name  = "A320",
77 		.fw = {
78 			[ADRENO_FW_PM4] = "a300_pm4.fw",
79 			[ADRENO_FW_PFP] = "a300_pfp.fw",
80 		},
81 		.gmem  = SZ_512K,
82 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
83 		.init  = a3xx_gpu_init,
84 	}, {
85 		.rev   = ADRENO_REV(3, 3, 0, ANY_ID),
86 		.revn  = 330,
87 		.name  = "A330",
88 		.fw = {
89 			[ADRENO_FW_PM4] = "a330_pm4.fw",
90 			[ADRENO_FW_PFP] = "a330_pfp.fw",
91 		},
92 		.gmem  = SZ_1M,
93 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
94 		.init  = a3xx_gpu_init,
95 	}, {
96 		.rev   = ADRENO_REV(4, 0, 5, ANY_ID),
97 		.revn  = 405,
98 		.name  = "A405",
99 		.fw = {
100 			[ADRENO_FW_PM4] = "a420_pm4.fw",
101 			[ADRENO_FW_PFP] = "a420_pfp.fw",
102 		},
103 		.gmem  = SZ_256K,
104 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
105 		.init  = a4xx_gpu_init,
106 	}, {
107 		.rev   = ADRENO_REV(4, 2, 0, ANY_ID),
108 		.revn  = 420,
109 		.name  = "A420",
110 		.fw = {
111 			[ADRENO_FW_PM4] = "a420_pm4.fw",
112 			[ADRENO_FW_PFP] = "a420_pfp.fw",
113 		},
114 		.gmem  = (SZ_1M + SZ_512K),
115 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
116 		.init  = a4xx_gpu_init,
117 	}, {
118 		.rev   = ADRENO_REV(4, 3, 0, ANY_ID),
119 		.revn  = 430,
120 		.name  = "A430",
121 		.fw = {
122 			[ADRENO_FW_PM4] = "a420_pm4.fw",
123 			[ADRENO_FW_PFP] = "a420_pfp.fw",
124 		},
125 		.gmem  = (SZ_1M + SZ_512K),
126 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
127 		.init  = a4xx_gpu_init,
128 	}, {
129 		.rev   = ADRENO_REV(5, 1, 0, ANY_ID),
130 		.revn = 510,
131 		.name = "A510",
132 		.fw = {
133 			[ADRENO_FW_PM4] = "a530_pm4.fw",
134 			[ADRENO_FW_PFP] = "a530_pfp.fw",
135 		},
136 		.gmem = SZ_256K,
137 		/*
138 		 * Increase inactive period to 250 to avoid bouncing
139 		 * the GDSC which appears to make it grumpy
140 		 */
141 		.inactive_period = 250,
142 		.init = a5xx_gpu_init,
143 	}, {
144 		.rev = ADRENO_REV(5, 3, 0, 2),
145 		.revn = 530,
146 		.name = "A530",
147 		.fw = {
148 			[ADRENO_FW_PM4] = "a530_pm4.fw",
149 			[ADRENO_FW_PFP] = "a530_pfp.fw",
150 			[ADRENO_FW_GPMU] = "a530v3_gpmu.fw2",
151 		},
152 		.gmem = SZ_1M,
153 		/*
154 		 * Increase inactive period to 250 to avoid bouncing
155 		 * the GDSC which appears to make it grumpy
156 		 */
157 		.inactive_period = 250,
158 		.quirks = ADRENO_QUIRK_TWO_PASS_USE_WFI |
159 			ADRENO_QUIRK_FAULT_DETECT_MASK,
160 		.init = a5xx_gpu_init,
161 		.zapfw = "a530_zap.mdt",
162 	}, {
163 		.rev = ADRENO_REV(5, 4, 0, 2),
164 		.revn = 540,
165 		.name = "A540",
166 		.fw = {
167 			[ADRENO_FW_PM4] = "a530_pm4.fw",
168 			[ADRENO_FW_PFP] = "a530_pfp.fw",
169 			[ADRENO_FW_GPMU] = "a540_gpmu.fw2",
170 		},
171 		.gmem = SZ_1M,
172 		/*
173 		 * Increase inactive period to 250 to avoid bouncing
174 		 * the GDSC which appears to make it grumpy
175 		 */
176 		.inactive_period = 250,
177 		.quirks = ADRENO_QUIRK_LMLOADKILL_DISABLE,
178 		.init = a5xx_gpu_init,
179 		.zapfw = "a540_zap.mdt",
180 	}, {
181 		.rev = ADRENO_REV(6, 1, 8, ANY_ID),
182 		.revn = 618,
183 		.name = "A618",
184 		.fw = {
185 			[ADRENO_FW_SQE] = "a630_sqe.fw",
186 			[ADRENO_FW_GMU] = "a630_gmu.bin",
187 		},
188 		.gmem = SZ_512K,
189 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
190 		.init = a6xx_gpu_init,
191 	}, {
192 		.rev = ADRENO_REV(6, 3, 0, ANY_ID),
193 		.revn = 630,
194 		.name = "A630",
195 		.fw = {
196 			[ADRENO_FW_SQE] = "a630_sqe.fw",
197 			[ADRENO_FW_GMU] = "a630_gmu.bin",
198 		},
199 		.gmem = SZ_1M,
200 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
201 		.init = a6xx_gpu_init,
202 		.zapfw = "a630_zap.mdt",
203 	}, {
204 		.rev = ADRENO_REV(6, 4, 0, ANY_ID),
205 		.revn = 640,
206 		.name = "A640",
207 		.fw = {
208 			[ADRENO_FW_SQE] = "a630_sqe.fw",
209 			[ADRENO_FW_GMU] = "a640_gmu.bin",
210 		},
211 		.gmem = SZ_1M,
212 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
213 		.init = a6xx_gpu_init,
214 		.zapfw = "a640_zap.mdt",
215 	}, {
216 		.rev = ADRENO_REV(6, 5, 0, ANY_ID),
217 		.revn = 650,
218 		.name = "A650",
219 		.fw = {
220 			[ADRENO_FW_SQE] = "a650_sqe.fw",
221 			[ADRENO_FW_GMU] = "a650_gmu.bin",
222 		},
223 		.gmem = SZ_1M + SZ_128K,
224 		.inactive_period = DRM_MSM_INACTIVE_PERIOD,
225 		.init = a6xx_gpu_init,
226 		.zapfw = "a650_zap.mdt",
227 	},
228 };
229 
230 MODULE_FIRMWARE("qcom/a300_pm4.fw");
231 MODULE_FIRMWARE("qcom/a300_pfp.fw");
232 MODULE_FIRMWARE("qcom/a330_pm4.fw");
233 MODULE_FIRMWARE("qcom/a330_pfp.fw");
234 MODULE_FIRMWARE("qcom/a420_pm4.fw");
235 MODULE_FIRMWARE("qcom/a420_pfp.fw");
236 MODULE_FIRMWARE("qcom/a530_pm4.fw");
237 MODULE_FIRMWARE("qcom/a530_pfp.fw");
238 MODULE_FIRMWARE("qcom/a530v3_gpmu.fw2");
239 MODULE_FIRMWARE("qcom/a530_zap.mdt");
240 MODULE_FIRMWARE("qcom/a530_zap.b00");
241 MODULE_FIRMWARE("qcom/a530_zap.b01");
242 MODULE_FIRMWARE("qcom/a530_zap.b02");
243 MODULE_FIRMWARE("qcom/a630_sqe.fw");
244 MODULE_FIRMWARE("qcom/a630_gmu.bin");
245 MODULE_FIRMWARE("qcom/a630_zap.mbn");
246 
247 static inline bool _rev_match(uint8_t entry, uint8_t id)
248 {
249 	return (entry == ANY_ID) || (entry == id);
250 }
251 
252 const struct adreno_info *adreno_info(struct adreno_rev rev)
253 {
254 	int i;
255 
256 	/* identify gpu: */
257 	for (i = 0; i < ARRAY_SIZE(gpulist); i++) {
258 		const struct adreno_info *info = &gpulist[i];
259 		if (_rev_match(info->rev.core, rev.core) &&
260 				_rev_match(info->rev.major, rev.major) &&
261 				_rev_match(info->rev.minor, rev.minor) &&
262 				_rev_match(info->rev.patchid, rev.patchid))
263 			return info;
264 	}
265 
266 	return NULL;
267 }
268 
269 struct msm_gpu *adreno_load_gpu(struct drm_device *dev)
270 {
271 	struct msm_drm_private *priv = dev->dev_private;
272 	struct platform_device *pdev = priv->gpu_pdev;
273 	struct msm_gpu *gpu = NULL;
274 	struct adreno_gpu *adreno_gpu;
275 	int ret;
276 
277 	if (pdev)
278 		gpu = platform_get_drvdata(pdev);
279 
280 	if (!gpu) {
281 		dev_err_once(dev->dev, "no GPU device was found\n");
282 		return NULL;
283 	}
284 
285 	adreno_gpu = to_adreno_gpu(gpu);
286 
287 	/*
288 	 * The number one reason for HW init to fail is if the firmware isn't
289 	 * loaded yet. Try that first and don't bother continuing on
290 	 * otherwise
291 	 */
292 
293 	ret = adreno_load_fw(adreno_gpu);
294 	if (ret)
295 		return NULL;
296 
297 	/* Make sure pm runtime is active and reset any previous errors */
298 	pm_runtime_set_active(&pdev->dev);
299 
300 	ret = pm_runtime_get_sync(&pdev->dev);
301 	if (ret < 0) {
302 		pm_runtime_put_sync(&pdev->dev);
303 		DRM_DEV_ERROR(dev->dev, "Couldn't power up the GPU: %d\n", ret);
304 		return NULL;
305 	}
306 
307 	mutex_lock(&dev->struct_mutex);
308 	ret = msm_gpu_hw_init(gpu);
309 	mutex_unlock(&dev->struct_mutex);
310 	pm_runtime_put_autosuspend(&pdev->dev);
311 	if (ret) {
312 		DRM_DEV_ERROR(dev->dev, "gpu hw init failed: %d\n", ret);
313 		return NULL;
314 	}
315 
316 #ifdef CONFIG_DEBUG_FS
317 	if (gpu->funcs->debugfs_init) {
318 		gpu->funcs->debugfs_init(gpu, dev->primary);
319 		gpu->funcs->debugfs_init(gpu, dev->render);
320 	}
321 #endif
322 
323 	return gpu;
324 }
325 
326 static void set_gpu_pdev(struct drm_device *dev,
327 		struct platform_device *pdev)
328 {
329 	struct msm_drm_private *priv = dev->dev_private;
330 	priv->gpu_pdev = pdev;
331 }
332 
333 static int find_chipid(struct device *dev, struct adreno_rev *rev)
334 {
335 	struct device_node *node = dev->of_node;
336 	const char *compat;
337 	int ret;
338 	u32 chipid;
339 
340 	/* first search the compat strings for qcom,adreno-XYZ.W: */
341 	ret = of_property_read_string_index(node, "compatible", 0, &compat);
342 	if (ret == 0) {
343 		unsigned int r, patch;
344 
345 		if (sscanf(compat, "qcom,adreno-%u.%u", &r, &patch) == 2 ||
346 		    sscanf(compat, "amd,imageon-%u.%u", &r, &patch) == 2) {
347 			rev->core = r / 100;
348 			r %= 100;
349 			rev->major = r / 10;
350 			r %= 10;
351 			rev->minor = r;
352 			rev->patchid = patch;
353 
354 			return 0;
355 		}
356 	}
357 
358 	/* and if that fails, fall back to legacy "qcom,chipid" property: */
359 	ret = of_property_read_u32(node, "qcom,chipid", &chipid);
360 	if (ret) {
361 		DRM_DEV_ERROR(dev, "could not parse qcom,chipid: %d\n", ret);
362 		return ret;
363 	}
364 
365 	rev->core = (chipid >> 24) & 0xff;
366 	rev->major = (chipid >> 16) & 0xff;
367 	rev->minor = (chipid >> 8) & 0xff;
368 	rev->patchid = (chipid & 0xff);
369 
370 	dev_warn(dev, "Using legacy qcom,chipid binding!\n");
371 	dev_warn(dev, "Use compatible qcom,adreno-%u%u%u.%u instead.\n",
372 		rev->core, rev->major, rev->minor, rev->patchid);
373 
374 	return 0;
375 }
376 
377 static int adreno_bind(struct device *dev, struct device *master, void *data)
378 {
379 	static struct adreno_platform_config config = {};
380 	const struct adreno_info *info;
381 	struct drm_device *drm = dev_get_drvdata(master);
382 	struct msm_drm_private *priv = drm->dev_private;
383 	struct msm_gpu *gpu;
384 	int ret;
385 
386 	ret = find_chipid(dev, &config.rev);
387 	if (ret)
388 		return ret;
389 
390 	dev->platform_data = &config;
391 	set_gpu_pdev(drm, to_platform_device(dev));
392 
393 	info = adreno_info(config.rev);
394 
395 	if (!info) {
396 		dev_warn(drm->dev, "Unknown GPU revision: %u.%u.%u.%u\n",
397 			config.rev.core, config.rev.major,
398 			config.rev.minor, config.rev.patchid);
399 		return -ENXIO;
400 	}
401 
402 	DBG("Found GPU: %u.%u.%u.%u", config.rev.core, config.rev.major,
403 		config.rev.minor, config.rev.patchid);
404 
405 	priv->is_a2xx = config.rev.core == 2;
406 
407 	gpu = info->init(drm);
408 	if (IS_ERR(gpu)) {
409 		dev_warn(drm->dev, "failed to load adreno gpu\n");
410 		return PTR_ERR(gpu);
411 	}
412 
413 	dev_set_drvdata(dev, gpu);
414 
415 	return 0;
416 }
417 
418 static void adreno_unbind(struct device *dev, struct device *master,
419 		void *data)
420 {
421 	struct msm_gpu *gpu = dev_get_drvdata(dev);
422 
423 	pm_runtime_force_suspend(dev);
424 	gpu->funcs->destroy(gpu);
425 
426 	set_gpu_pdev(dev_get_drvdata(master), NULL);
427 }
428 
429 static const struct component_ops a3xx_ops = {
430 		.bind   = adreno_bind,
431 		.unbind = adreno_unbind,
432 };
433 
434 static void adreno_device_register_headless(void)
435 {
436 	/* on imx5, we don't have a top-level mdp/dpu node
437 	 * this creates a dummy node for the driver for that case
438 	 */
439 	struct platform_device_info dummy_info = {
440 		.parent = NULL,
441 		.name = "msm",
442 		.id = -1,
443 		.res = NULL,
444 		.num_res = 0,
445 		.data = NULL,
446 		.size_data = 0,
447 		.dma_mask = ~0,
448 	};
449 	platform_device_register_full(&dummy_info);
450 }
451 
452 static int adreno_probe(struct platform_device *pdev)
453 {
454 
455 	int ret;
456 
457 	ret = component_add(&pdev->dev, &a3xx_ops);
458 	if (ret)
459 		return ret;
460 
461 	if (of_device_is_compatible(pdev->dev.of_node, "amd,imageon"))
462 		adreno_device_register_headless();
463 
464 	return 0;
465 }
466 
467 static int adreno_remove(struct platform_device *pdev)
468 {
469 	component_del(&pdev->dev, &a3xx_ops);
470 	return 0;
471 }
472 
473 static const struct of_device_id dt_match[] = {
474 	{ .compatible = "qcom,adreno" },
475 	{ .compatible = "qcom,adreno-3xx" },
476 	/* for compatibility with imx5 gpu: */
477 	{ .compatible = "amd,imageon" },
478 	/* for backwards compat w/ downstream kgsl DT files: */
479 	{ .compatible = "qcom,kgsl-3d0" },
480 	{}
481 };
482 
483 #ifdef CONFIG_PM
484 static int adreno_resume(struct device *dev)
485 {
486 	struct platform_device *pdev = to_platform_device(dev);
487 	struct msm_gpu *gpu = platform_get_drvdata(pdev);
488 
489 	return gpu->funcs->pm_resume(gpu);
490 }
491 
492 static int adreno_suspend(struct device *dev)
493 {
494 	struct platform_device *pdev = to_platform_device(dev);
495 	struct msm_gpu *gpu = platform_get_drvdata(pdev);
496 
497 	return gpu->funcs->pm_suspend(gpu);
498 }
499 #endif
500 
501 static const struct dev_pm_ops adreno_pm_ops = {
502 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, pm_runtime_force_resume)
503 	SET_RUNTIME_PM_OPS(adreno_suspend, adreno_resume, NULL)
504 };
505 
506 static struct platform_driver adreno_driver = {
507 	.probe = adreno_probe,
508 	.remove = adreno_remove,
509 	.driver = {
510 		.name = "adreno",
511 		.of_match_table = dt_match,
512 		.pm = &adreno_pm_ops,
513 	},
514 };
515 
516 void __init adreno_register(void)
517 {
518 	platform_driver_register(&adreno_driver);
519 }
520 
521 void __exit adreno_unregister(void)
522 {
523 	platform_driver_unregister(&adreno_driver);
524 }
525