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 	if (WARN(pfdev->comp->num_supplies > ARRAY_SIZE(pfdev->regulators),
94 			"Too many supplies in compatible structure.\n"))
95 		return -EINVAL;
96 
97 	for (i = 0; i < pfdev->comp->num_supplies; i++)
98 		pfdev->regulators[i].supply = pfdev->comp->supply_names[i];
99 
100 	ret = devm_regulator_bulk_get(pfdev->dev,
101 				      pfdev->comp->num_supplies,
102 				      pfdev->regulators);
103 	if (ret < 0) {
104 		if (ret != -EPROBE_DEFER)
105 			dev_err(pfdev->dev, "failed to get regulators: %d\n",
106 				ret);
107 		return ret;
108 	}
109 
110 	ret = regulator_bulk_enable(pfdev->comp->num_supplies,
111 				    pfdev->regulators);
112 	if (ret < 0) {
113 		dev_err(pfdev->dev, "failed to enable regulators: %d\n", ret);
114 		return ret;
115 	}
116 
117 	return 0;
118 }
119 
120 static void panfrost_regulator_fini(struct panfrost_device *pfdev)
121 {
122 	regulator_bulk_disable(pfdev->comp->num_supplies,
123 			pfdev->regulators);
124 }
125 
126 static void panfrost_pm_domain_fini(struct panfrost_device *pfdev)
127 {
128 	int i;
129 
130 	for (i = 0; i < ARRAY_SIZE(pfdev->pm_domain_devs); i++) {
131 		if (!pfdev->pm_domain_devs[i])
132 			break;
133 
134 		if (pfdev->pm_domain_links[i])
135 			device_link_del(pfdev->pm_domain_links[i]);
136 
137 		dev_pm_domain_detach(pfdev->pm_domain_devs[i], true);
138 	}
139 }
140 
141 static int panfrost_pm_domain_init(struct panfrost_device *pfdev)
142 {
143 	int err;
144 	int i, num_domains;
145 
146 	num_domains = of_count_phandle_with_args(pfdev->dev->of_node,
147 						 "power-domains",
148 						 "#power-domain-cells");
149 
150 	/*
151 	 * Single domain is handled by the core, and, if only a single power
152 	 * the power domain is requested, the property is optional.
153 	 */
154 	if (num_domains < 2 && pfdev->comp->num_pm_domains < 2)
155 		return 0;
156 
157 	if (num_domains != pfdev->comp->num_pm_domains) {
158 		dev_err(pfdev->dev,
159 			"Incorrect number of power domains: %d provided, %d needed\n",
160 			num_domains, pfdev->comp->num_pm_domains);
161 		return -EINVAL;
162 	}
163 
164 	if (WARN(num_domains > ARRAY_SIZE(pfdev->pm_domain_devs),
165 			"Too many supplies in compatible structure.\n"))
166 		return -EINVAL;
167 
168 	for (i = 0; i < num_domains; i++) {
169 		pfdev->pm_domain_devs[i] =
170 			dev_pm_domain_attach_by_name(pfdev->dev,
171 					pfdev->comp->pm_domain_names[i]);
172 		if (IS_ERR_OR_NULL(pfdev->pm_domain_devs[i])) {
173 			err = PTR_ERR(pfdev->pm_domain_devs[i]) ? : -ENODATA;
174 			pfdev->pm_domain_devs[i] = NULL;
175 			dev_err(pfdev->dev,
176 				"failed to get pm-domain %s(%d): %d\n",
177 				pfdev->comp->pm_domain_names[i], i, err);
178 			goto err;
179 		}
180 
181 		pfdev->pm_domain_links[i] = device_link_add(pfdev->dev,
182 				pfdev->pm_domain_devs[i], DL_FLAG_PM_RUNTIME |
183 				DL_FLAG_STATELESS | DL_FLAG_RPM_ACTIVE);
184 		if (!pfdev->pm_domain_links[i]) {
185 			dev_err(pfdev->pm_domain_devs[i],
186 				"adding device link failed!\n");
187 			err = -ENODEV;
188 			goto err;
189 		}
190 	}
191 
192 	return 0;
193 
194 err:
195 	panfrost_pm_domain_fini(pfdev);
196 	return err;
197 }
198 
199 int panfrost_device_init(struct panfrost_device *pfdev)
200 {
201 	int err;
202 	struct resource *res;
203 
204 	mutex_init(&pfdev->sched_lock);
205 	mutex_init(&pfdev->reset_lock);
206 	INIT_LIST_HEAD(&pfdev->scheduled_jobs);
207 	INIT_LIST_HEAD(&pfdev->as_lru_list);
208 
209 	spin_lock_init(&pfdev->as_lock);
210 
211 	err = panfrost_clk_init(pfdev);
212 	if (err) {
213 		dev_err(pfdev->dev, "clk init failed %d\n", err);
214 		return err;
215 	}
216 
217 	err = panfrost_regulator_init(pfdev);
218 	if (err)
219 		goto err_out0;
220 
221 	err = panfrost_reset_init(pfdev);
222 	if (err) {
223 		dev_err(pfdev->dev, "reset init failed %d\n", err);
224 		goto err_out1;
225 	}
226 
227 	err = panfrost_pm_domain_init(pfdev);
228 	if (err)
229 		goto err_out2;
230 
231 	res = platform_get_resource(pfdev->pdev, IORESOURCE_MEM, 0);
232 	pfdev->iomem = devm_ioremap_resource(pfdev->dev, res);
233 	if (IS_ERR(pfdev->iomem)) {
234 		dev_err(pfdev->dev, "failed to ioremap iomem\n");
235 		err = PTR_ERR(pfdev->iomem);
236 		goto err_out3;
237 	}
238 
239 	err = panfrost_gpu_init(pfdev);
240 	if (err)
241 		goto err_out3;
242 
243 	err = panfrost_mmu_init(pfdev);
244 	if (err)
245 		goto err_out4;
246 
247 	err = panfrost_job_init(pfdev);
248 	if (err)
249 		goto err_out5;
250 
251 	err = panfrost_perfcnt_init(pfdev);
252 	if (err)
253 		goto err_out6;
254 
255 	return 0;
256 err_out6:
257 	panfrost_job_fini(pfdev);
258 err_out5:
259 	panfrost_mmu_fini(pfdev);
260 err_out4:
261 	panfrost_gpu_fini(pfdev);
262 err_out3:
263 	panfrost_pm_domain_fini(pfdev);
264 err_out2:
265 	panfrost_reset_fini(pfdev);
266 err_out1:
267 	panfrost_regulator_fini(pfdev);
268 err_out0:
269 	panfrost_clk_fini(pfdev);
270 	return err;
271 }
272 
273 void panfrost_device_fini(struct panfrost_device *pfdev)
274 {
275 	panfrost_perfcnt_fini(pfdev);
276 	panfrost_job_fini(pfdev);
277 	panfrost_mmu_fini(pfdev);
278 	panfrost_gpu_fini(pfdev);
279 	panfrost_pm_domain_fini(pfdev);
280 	panfrost_reset_fini(pfdev);
281 	panfrost_regulator_fini(pfdev);
282 	panfrost_clk_fini(pfdev);
283 }
284 
285 const char *panfrost_exception_name(struct panfrost_device *pfdev, u32 exception_code)
286 {
287 	switch (exception_code) {
288 		/* Non-Fault Status code */
289 	case 0x00: return "NOT_STARTED/IDLE/OK";
290 	case 0x01: return "DONE";
291 	case 0x02: return "INTERRUPTED";
292 	case 0x03: return "STOPPED";
293 	case 0x04: return "TERMINATED";
294 	case 0x08: return "ACTIVE";
295 		/* Job exceptions */
296 	case 0x40: return "JOB_CONFIG_FAULT";
297 	case 0x41: return "JOB_POWER_FAULT";
298 	case 0x42: return "JOB_READ_FAULT";
299 	case 0x43: return "JOB_WRITE_FAULT";
300 	case 0x44: return "JOB_AFFINITY_FAULT";
301 	case 0x48: return "JOB_BUS_FAULT";
302 	case 0x50: return "INSTR_INVALID_PC";
303 	case 0x51: return "INSTR_INVALID_ENC";
304 	case 0x52: return "INSTR_TYPE_MISMATCH";
305 	case 0x53: return "INSTR_OPERAND_FAULT";
306 	case 0x54: return "INSTR_TLS_FAULT";
307 	case 0x55: return "INSTR_BARRIER_FAULT";
308 	case 0x56: return "INSTR_ALIGN_FAULT";
309 	case 0x58: return "DATA_INVALID_FAULT";
310 	case 0x59: return "TILE_RANGE_FAULT";
311 	case 0x5A: return "ADDR_RANGE_FAULT";
312 	case 0x60: return "OUT_OF_MEMORY";
313 		/* GPU exceptions */
314 	case 0x80: return "DELAYED_BUS_FAULT";
315 	case 0x88: return "SHAREABILITY_FAULT";
316 		/* MMU exceptions */
317 	case 0xC1: return "TRANSLATION_FAULT_LEVEL1";
318 	case 0xC2: return "TRANSLATION_FAULT_LEVEL2";
319 	case 0xC3: return "TRANSLATION_FAULT_LEVEL3";
320 	case 0xC4: return "TRANSLATION_FAULT_LEVEL4";
321 	case 0xC8: return "PERMISSION_FAULT";
322 	case 0xC9 ... 0xCF: return "PERMISSION_FAULT";
323 	case 0xD1: return "TRANSTAB_BUS_FAULT_LEVEL1";
324 	case 0xD2: return "TRANSTAB_BUS_FAULT_LEVEL2";
325 	case 0xD3: return "TRANSTAB_BUS_FAULT_LEVEL3";
326 	case 0xD4: return "TRANSTAB_BUS_FAULT_LEVEL4";
327 	case 0xD8: return "ACCESS_FLAG";
328 	case 0xD9 ... 0xDF: return "ACCESS_FLAG";
329 	case 0xE0 ... 0xE7: return "ADDRESS_SIZE_FAULT";
330 	case 0xE8 ... 0xEF: return "MEMORY_ATTRIBUTES_FAULT";
331 	}
332 
333 	return "UNKNOWN";
334 }
335 
336 void panfrost_device_reset(struct panfrost_device *pfdev)
337 {
338 	panfrost_gpu_soft_reset(pfdev);
339 
340 	panfrost_gpu_power_on(pfdev);
341 	panfrost_mmu_reset(pfdev);
342 	panfrost_job_enable_interrupts(pfdev);
343 }
344 
345 #ifdef CONFIG_PM
346 int panfrost_device_resume(struct device *dev)
347 {
348 	struct platform_device *pdev = to_platform_device(dev);
349 	struct panfrost_device *pfdev = platform_get_drvdata(pdev);
350 
351 	panfrost_device_reset(pfdev);
352 	panfrost_devfreq_resume(pfdev);
353 
354 	return 0;
355 }
356 
357 int panfrost_device_suspend(struct device *dev)
358 {
359 	struct platform_device *pdev = to_platform_device(dev);
360 	struct panfrost_device *pfdev = platform_get_drvdata(pdev);
361 
362 	if (!panfrost_job_is_idle(pfdev))
363 		return -EBUSY;
364 
365 	panfrost_devfreq_suspend(pfdev);
366 	panfrost_gpu_power_off(pfdev);
367 
368 	return 0;
369 }
370 #endif
371