xref: /linux/drivers/gpu/drm/msm/adreno/adreno_device.c (revision bca5cfbb694d66a1c482d0c347eee80f6afbc870)
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 bool hang_debug = false;
12 MODULE_PARM_DESC(hang_debug, "Dump registers when hang is detected (can be slow!)");
13 module_param_named(hang_debug, hang_debug, bool, 0600);
14 
15 bool snapshot_debugbus = false;
16 MODULE_PARM_DESC(snapshot_debugbus, "Include debugbus sections in GPU devcoredump (if not fused off)");
17 module_param_named(snapshot_debugbus, snapshot_debugbus, bool, 0600);
18 
19 bool allow_vram_carveout = false;
20 MODULE_PARM_DESC(allow_vram_carveout, "Allow using VRAM Carveout, in place of IOMMU");
21 module_param_named(allow_vram_carveout, allow_vram_carveout, bool, 0600);
22 
23 int enable_preemption = -1;
24 MODULE_PARM_DESC(enable_preemption, "Enable preemption (A7xx only) (1=on , 0=disable, -1=auto (default))");
25 module_param(enable_preemption, int, 0600);
26 
27 bool disable_acd;
28 MODULE_PARM_DESC(disable_acd, "Forcefully disable GPU ACD");
29 module_param_unsafe(disable_acd, bool, 0400);
30 
31 extern const struct adreno_gpulist a2xx_gpulist;
32 extern const struct adreno_gpulist a3xx_gpulist;
33 extern const struct adreno_gpulist a4xx_gpulist;
34 extern const struct adreno_gpulist a5xx_gpulist;
35 extern const struct adreno_gpulist a6xx_gpulist;
36 extern const struct adreno_gpulist a7xx_gpulist;
37 
38 static const struct adreno_gpulist *gpulists[] = {
39 	&a2xx_gpulist,
40 	&a3xx_gpulist,
41 	&a4xx_gpulist,
42 	&a5xx_gpulist,
43 	&a6xx_gpulist,
44 	&a7xx_gpulist,
45 };
46 
47 static const struct adreno_info *adreno_info(uint32_t chip_id)
48 {
49 	/* identify gpu: */
50 	for (int i = 0; i < ARRAY_SIZE(gpulists); i++) {
51 		for (int j = 0; j < gpulists[i]->gpus_count; j++) {
52 			const struct adreno_info *info = &gpulists[i]->gpus[j];
53 
54 			if (info->machine && !of_machine_is_compatible(info->machine))
55 				continue;
56 
57 			for (int k = 0; info->chip_ids[k]; k++)
58 				if (info->chip_ids[k] == chip_id)
59 					return info;
60 		}
61 	}
62 
63 	return NULL;
64 }
65 
66 struct msm_gpu *adreno_load_gpu(struct drm_device *dev)
67 {
68 	struct msm_drm_private *priv = dev->dev_private;
69 	struct platform_device *pdev = priv->gpu_pdev;
70 	struct msm_gpu *gpu = NULL;
71 	struct adreno_gpu *adreno_gpu;
72 	int ret;
73 
74 	if (pdev)
75 		gpu = dev_to_gpu(&pdev->dev);
76 
77 	if (!gpu) {
78 		dev_err_once(dev->dev, "no GPU device was found\n");
79 		return NULL;
80 	}
81 
82 	adreno_gpu = to_adreno_gpu(gpu);
83 
84 	/*
85 	 * The number one reason for HW init to fail is if the firmware isn't
86 	 * loaded yet. Try that first and don't bother continuing on
87 	 * otherwise
88 	 */
89 
90 	ret = adreno_load_fw(adreno_gpu);
91 	if (ret)
92 		return NULL;
93 
94 	if (gpu->funcs->ucode_load) {
95 		ret = gpu->funcs->ucode_load(gpu);
96 		if (ret)
97 			return NULL;
98 	}
99 
100 	/*
101 	 * Now that we have firmware loaded, and are ready to begin
102 	 * booting the gpu, go ahead and enable runpm:
103 	 */
104 	pm_runtime_enable(&pdev->dev);
105 
106 	ret = pm_runtime_get_sync(&pdev->dev);
107 	if (ret < 0) {
108 		pm_runtime_put_noidle(&pdev->dev);
109 		DRM_DEV_ERROR(dev->dev, "Couldn't power up the GPU: %d\n", ret);
110 		goto err_disable_rpm;
111 	}
112 
113 	mutex_lock(&gpu->lock);
114 	ret = msm_gpu_hw_init(gpu);
115 	mutex_unlock(&gpu->lock);
116 	if (ret) {
117 		DRM_DEV_ERROR(dev->dev, "gpu hw init failed: %d\n", ret);
118 		goto err_put_rpm;
119 	}
120 
121 	pm_runtime_put_autosuspend(&pdev->dev);
122 
123 #ifdef CONFIG_DEBUG_FS
124 	if (gpu->funcs->debugfs_init) {
125 		gpu->funcs->debugfs_init(gpu, dev->primary);
126 		gpu->funcs->debugfs_init(gpu, dev->render);
127 	}
128 #endif
129 
130 	return gpu;
131 
132 err_put_rpm:
133 	pm_runtime_put_sync_suspend(&pdev->dev);
134 err_disable_rpm:
135 	pm_runtime_disable(&pdev->dev);
136 
137 	return NULL;
138 }
139 
140 static int find_chipid(struct device *dev, uint32_t *chipid)
141 {
142 	struct device_node *node = dev->of_node;
143 	const char *compat;
144 	int ret;
145 
146 	/* first search the compat strings for qcom,adreno-XYZ.W: */
147 	ret = of_property_read_string_index(node, "compatible", 0, &compat);
148 	if (ret == 0) {
149 		unsigned int r, patch;
150 
151 		if (sscanf(compat, "qcom,adreno-%u.%u", &r, &patch) == 2 ||
152 		    sscanf(compat, "amd,imageon-%u.%u", &r, &patch) == 2) {
153 			uint32_t core, major, minor;
154 
155 			core = r / 100;
156 			r %= 100;
157 			major = r / 10;
158 			r %= 10;
159 			minor = r;
160 
161 			*chipid = (core << 24) |
162 				(major << 16) |
163 				(minor << 8) |
164 				patch;
165 
166 			return 0;
167 		}
168 
169 		if (sscanf(compat, "qcom,adreno-%08x", chipid) == 1)
170 			return 0;
171 	}
172 
173 	/* and if that fails, fall back to legacy "qcom,chipid" property: */
174 	ret = of_property_read_u32(node, "qcom,chipid", chipid);
175 	if (ret) {
176 		DRM_DEV_ERROR(dev, "could not parse qcom,chipid: %d\n", ret);
177 		return ret;
178 	}
179 
180 	dev_warn(dev, "Using legacy qcom,chipid binding!\n");
181 
182 	return 0;
183 }
184 
185 static int adreno_bind(struct device *dev, struct device *master, void *data)
186 {
187 	static struct adreno_platform_config config = {};
188 	const struct adreno_info *info;
189 	struct msm_drm_private *priv = dev_get_drvdata(master);
190 	struct drm_device *drm = priv->dev;
191 	struct msm_gpu *gpu;
192 	int ret;
193 
194 	ret = find_chipid(dev, &config.chip_id);
195 	if (ret)
196 		return ret;
197 
198 	dev->platform_data = &config;
199 	priv->gpu_pdev = to_platform_device(dev);
200 
201 	info = adreno_info(config.chip_id);
202 	if (!info) {
203 		dev_warn(drm->dev, "Unknown GPU revision: %"ADRENO_CHIPID_FMT"\n",
204 			ADRENO_CHIPID_ARGS(config.chip_id));
205 		return -ENXIO;
206 	}
207 
208 	config.info = info;
209 
210 	DBG("Found GPU: %"ADRENO_CHIPID_FMT, ADRENO_CHIPID_ARGS(config.chip_id));
211 
212 	priv->is_a2xx = info->family < ADRENO_3XX;
213 	priv->has_cached_coherent =
214 		!!(info->quirks & ADRENO_QUIRK_HAS_CACHED_COHERENT);
215 
216 	gpu = info->init(drm);
217 	if (IS_ERR(gpu)) {
218 		dev_warn(drm->dev, "failed to load adreno gpu\n");
219 		return PTR_ERR(gpu);
220 	}
221 
222 	ret = dev_pm_opp_of_find_icc_paths(dev, NULL);
223 	if (ret)
224 		return ret;
225 
226 	return 0;
227 }
228 
229 static int adreno_system_suspend(struct device *dev);
230 static void adreno_unbind(struct device *dev, struct device *master,
231 		void *data)
232 {
233 	struct msm_drm_private *priv = dev_get_drvdata(master);
234 	struct msm_gpu *gpu = dev_to_gpu(dev);
235 
236 	if (pm_runtime_enabled(dev))
237 		WARN_ON_ONCE(adreno_system_suspend(dev));
238 	gpu->funcs->destroy(gpu);
239 
240 	priv->gpu_pdev = NULL;
241 }
242 
243 static const struct component_ops a3xx_ops = {
244 	.bind   = adreno_bind,
245 	.unbind = adreno_unbind,
246 };
247 
248 static void adreno_device_register_headless(void)
249 {
250 	/* on imx5, we don't have a top-level mdp/dpu node
251 	 * this creates a dummy node for the driver for that case
252 	 */
253 	struct platform_device_info dummy_info = {
254 		.parent = NULL,
255 		.name = "msm",
256 		.id = -1,
257 		.res = NULL,
258 		.num_res = 0,
259 		.data = NULL,
260 		.size_data = 0,
261 		.dma_mask = ~0,
262 	};
263 	platform_device_register_full(&dummy_info);
264 }
265 
266 static int adreno_probe(struct platform_device *pdev)
267 {
268 
269 	int ret;
270 
271 	ret = component_add(&pdev->dev, &a3xx_ops);
272 	if (ret)
273 		return ret;
274 
275 	if (of_device_is_compatible(pdev->dev.of_node, "amd,imageon"))
276 		adreno_device_register_headless();
277 
278 	return 0;
279 }
280 
281 static void adreno_remove(struct platform_device *pdev)
282 {
283 	component_del(&pdev->dev, &a3xx_ops);
284 }
285 
286 static void adreno_shutdown(struct platform_device *pdev)
287 {
288 	WARN_ON_ONCE(adreno_system_suspend(&pdev->dev));
289 }
290 
291 static const struct of_device_id dt_match[] = {
292 	{ .compatible = "qcom,adreno" },
293 	{ .compatible = "qcom,adreno-3xx" },
294 	/* for compatibility with imx5 gpu: */
295 	{ .compatible = "amd,imageon" },
296 	/* for backwards compat w/ downstream kgsl DT files: */
297 	{ .compatible = "qcom,kgsl-3d0" },
298 	{}
299 };
300 
301 static int adreno_runtime_resume(struct device *dev)
302 {
303 	struct msm_gpu *gpu = dev_to_gpu(dev);
304 
305 	return gpu->funcs->pm_resume(gpu);
306 }
307 
308 static int adreno_runtime_suspend(struct device *dev)
309 {
310 	struct msm_gpu *gpu = dev_to_gpu(dev);
311 
312 	/*
313 	 * We should be holding a runpm ref, which will prevent
314 	 * runtime suspend.  In the system suspend path, we've
315 	 * already waited for active jobs to complete.
316 	 */
317 	WARN_ON_ONCE(gpu->active_submits);
318 
319 	return gpu->funcs->pm_suspend(gpu);
320 }
321 
322 static void suspend_scheduler(struct msm_gpu *gpu)
323 {
324 	int i;
325 
326 	/*
327 	 * Shut down the scheduler before we force suspend, so that
328 	 * suspend isn't racing with scheduler kthread feeding us
329 	 * more work.
330 	 *
331 	 * Note, we just want to park the thread, and let any jobs
332 	 * that are already on the hw queue complete normally, as
333 	 * opposed to the drm_sched_stop() path used for handling
334 	 * faulting/timed-out jobs.  We can't really cancel any jobs
335 	 * already on the hw queue without racing with the GPU.
336 	 */
337 	for (i = 0; i < gpu->nr_rings; i++) {
338 		struct drm_gpu_scheduler *sched = &gpu->rb[i]->sched;
339 
340 		drm_sched_wqueue_stop(sched);
341 	}
342 }
343 
344 static void resume_scheduler(struct msm_gpu *gpu)
345 {
346 	int i;
347 
348 	for (i = 0; i < gpu->nr_rings; i++) {
349 		struct drm_gpu_scheduler *sched = &gpu->rb[i]->sched;
350 
351 		drm_sched_wqueue_start(sched);
352 	}
353 }
354 
355 static int adreno_system_suspend(struct device *dev)
356 {
357 	struct msm_gpu *gpu = dev_to_gpu(dev);
358 	int remaining, ret;
359 
360 	if (!gpu)
361 		return 0;
362 
363 	suspend_scheduler(gpu);
364 
365 	remaining = wait_event_timeout(gpu->retire_event,
366 				       gpu->active_submits == 0,
367 				       msecs_to_jiffies(1000));
368 	if (remaining == 0) {
369 		dev_err(dev, "Timeout waiting for GPU to suspend\n");
370 		ret = -EBUSY;
371 		goto out;
372 	}
373 
374 	ret = pm_runtime_force_suspend(dev);
375 out:
376 	if (ret)
377 		resume_scheduler(gpu);
378 
379 	return ret;
380 }
381 
382 static int adreno_system_resume(struct device *dev)
383 {
384 	struct msm_gpu *gpu = dev_to_gpu(dev);
385 
386 	if (!gpu)
387 		return 0;
388 
389 	resume_scheduler(gpu);
390 	return pm_runtime_force_resume(dev);
391 }
392 
393 static const struct dev_pm_ops adreno_pm_ops = {
394 	SYSTEM_SLEEP_PM_OPS(adreno_system_suspend, adreno_system_resume)
395 	RUNTIME_PM_OPS(adreno_runtime_suspend, adreno_runtime_resume, NULL)
396 };
397 
398 static struct platform_driver adreno_driver = {
399 	.probe = adreno_probe,
400 	.remove = adreno_remove,
401 	.shutdown = adreno_shutdown,
402 	.driver = {
403 		.name = "adreno",
404 		.of_match_table = dt_match,
405 		.pm = &adreno_pm_ops,
406 	},
407 };
408 
409 void __init adreno_register(void)
410 {
411 	platform_driver_register(&adreno_driver);
412 }
413 
414 void __exit adreno_unregister(void)
415 {
416 	platform_driver_unregister(&adreno_driver);
417 }
418