• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
panfrost_reset_init(struct panfrost_device * pfdev)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 
panfrost_reset_fini(struct panfrost_device * pfdev)36 static void panfrost_reset_fini(struct panfrost_device *pfdev)
37 {
38 	reset_control_assert(pfdev->rstc);
39 }
40 
panfrost_clk_init(struct panfrost_device * pfdev)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 		err = PTR_ERR(pfdev->bus_clock);
64 		goto disable_clock;
65 	}
66 
67 	if (pfdev->bus_clock) {
68 		rate = clk_get_rate(pfdev->bus_clock);
69 		dev_info(pfdev->dev, "bus_clock rate = %lu\n", rate);
70 
71 		err = clk_prepare_enable(pfdev->bus_clock);
72 		if (err)
73 			goto disable_clock;
74 	}
75 
76 	return 0;
77 
78 disable_clock:
79 	clk_disable_unprepare(pfdev->clock);
80 
81 	return err;
82 }
83 
panfrost_clk_fini(struct panfrost_device * pfdev)84 static void panfrost_clk_fini(struct panfrost_device *pfdev)
85 {
86 	clk_disable_unprepare(pfdev->bus_clock);
87 	clk_disable_unprepare(pfdev->clock);
88 }
89 
panfrost_regulator_init(struct panfrost_device * pfdev)90 static int panfrost_regulator_init(struct panfrost_device *pfdev)
91 {
92 	int ret, i;
93 
94 	pfdev->regulators = devm_kcalloc(pfdev->dev, pfdev->comp->num_supplies,
95 					 sizeof(*pfdev->regulators),
96 					 GFP_KERNEL);
97 	if (!pfdev->regulators)
98 		return -ENOMEM;
99 
100 	for (i = 0; i < pfdev->comp->num_supplies; i++)
101 		pfdev->regulators[i].supply = pfdev->comp->supply_names[i];
102 
103 	ret = devm_regulator_bulk_get(pfdev->dev,
104 				      pfdev->comp->num_supplies,
105 				      pfdev->regulators);
106 	if (ret < 0) {
107 		if (ret != -EPROBE_DEFER)
108 			dev_err(pfdev->dev, "failed to get regulators: %d\n",
109 				ret);
110 		return ret;
111 	}
112 
113 	ret = regulator_bulk_enable(pfdev->comp->num_supplies,
114 				    pfdev->regulators);
115 	if (ret < 0) {
116 		dev_err(pfdev->dev, "failed to enable regulators: %d\n", ret);
117 		return ret;
118 	}
119 
120 	return 0;
121 }
122 
panfrost_regulator_fini(struct panfrost_device * pfdev)123 static void panfrost_regulator_fini(struct panfrost_device *pfdev)
124 {
125 	if (!pfdev->regulators)
126 		return;
127 
128 	regulator_bulk_disable(pfdev->comp->num_supplies, pfdev->regulators);
129 }
130 
panfrost_pm_domain_fini(struct panfrost_device * pfdev)131 static void panfrost_pm_domain_fini(struct panfrost_device *pfdev)
132 {
133 	int i;
134 
135 	for (i = 0; i < ARRAY_SIZE(pfdev->pm_domain_devs); i++) {
136 		if (!pfdev->pm_domain_devs[i])
137 			break;
138 
139 		if (pfdev->pm_domain_links[i])
140 			device_link_del(pfdev->pm_domain_links[i]);
141 
142 		dev_pm_domain_detach(pfdev->pm_domain_devs[i], true);
143 	}
144 }
145 
panfrost_pm_domain_init(struct panfrost_device * pfdev)146 static int panfrost_pm_domain_init(struct panfrost_device *pfdev)
147 {
148 	int err;
149 	int i, num_domains;
150 
151 	num_domains = of_count_phandle_with_args(pfdev->dev->of_node,
152 						 "power-domains",
153 						 "#power-domain-cells");
154 
155 	/*
156 	 * Single domain is handled by the core, and, if only a single power
157 	 * the power domain is requested, the property is optional.
158 	 */
159 	if (num_domains < 2 && pfdev->comp->num_pm_domains < 2)
160 		return 0;
161 
162 	if (num_domains != pfdev->comp->num_pm_domains) {
163 		dev_err(pfdev->dev,
164 			"Incorrect number of power domains: %d provided, %d needed\n",
165 			num_domains, pfdev->comp->num_pm_domains);
166 		return -EINVAL;
167 	}
168 
169 	if (WARN(num_domains > ARRAY_SIZE(pfdev->pm_domain_devs),
170 			"Too many supplies in compatible structure.\n"))
171 		return -EINVAL;
172 
173 	for (i = 0; i < num_domains; i++) {
174 		pfdev->pm_domain_devs[i] =
175 			dev_pm_domain_attach_by_name(pfdev->dev,
176 					pfdev->comp->pm_domain_names[i]);
177 		if (IS_ERR_OR_NULL(pfdev->pm_domain_devs[i])) {
178 			err = PTR_ERR(pfdev->pm_domain_devs[i]) ? : -ENODATA;
179 			pfdev->pm_domain_devs[i] = NULL;
180 			dev_err(pfdev->dev,
181 				"failed to get pm-domain %s(%d): %d\n",
182 				pfdev->comp->pm_domain_names[i], i, err);
183 			goto err;
184 		}
185 
186 		pfdev->pm_domain_links[i] = device_link_add(pfdev->dev,
187 				pfdev->pm_domain_devs[i], DL_FLAG_PM_RUNTIME |
188 				DL_FLAG_STATELESS | DL_FLAG_RPM_ACTIVE);
189 		if (!pfdev->pm_domain_links[i]) {
190 			dev_err(pfdev->pm_domain_devs[i],
191 				"adding device link failed!\n");
192 			err = -ENODEV;
193 			goto err;
194 		}
195 	}
196 
197 	return 0;
198 
199 err:
200 	panfrost_pm_domain_fini(pfdev);
201 	return err;
202 }
203 
panfrost_device_init(struct panfrost_device * pfdev)204 int panfrost_device_init(struct panfrost_device *pfdev)
205 {
206 	int err;
207 	struct resource *res;
208 
209 	mutex_init(&pfdev->sched_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 
panfrost_device_fini(struct panfrost_device * pfdev)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 
panfrost_exception_name(struct panfrost_device * pfdev,u32 exception_code)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 
panfrost_device_reset(struct panfrost_device * pfdev)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
panfrost_device_resume(struct device * dev)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 
panfrost_device_suspend(struct device * dev)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