Commit | Line | Data |
---|---|---|
a3d4d643 GL |
1 | /* |
2 | * PS3 system bus driver. | |
3 | * | |
4 | * Copyright (C) 2006 Sony Computer Entertainment Inc. | |
5 | * Copyright 2006 Sony Corp. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; version 2 of the License. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | */ | |
20 | ||
21 | #include <linux/kernel.h> | |
22 | #include <linux/init.h> | |
23 | #include <linux/module.h> | |
24 | #include <linux/dma-mapping.h> | |
25 | #include <linux/err.h> | |
26 | ||
27 | #include <asm/udbg.h> | |
28 | #include <asm/ps3.h> | |
29 | #include <asm/lv1call.h> | |
e22ba7e3 | 30 | #include <asm/firmware.h> |
a3d4d643 GL |
31 | |
32 | #define dump_mmio_region(_a) _dump_mmio_region(_a, __func__, __LINE__) | |
33 | static void _dump_mmio_region(const struct ps3_mmio_region* r, | |
34 | const char* func, int line) | |
35 | { | |
36 | pr_debug("%s:%d: dev %u:%u\n", func, line, r->did.bus_id, | |
37 | r->did.dev_id); | |
38 | pr_debug("%s:%d: bus_addr %lxh\n", func, line, r->bus_addr); | |
39 | pr_debug("%s:%d: len %lxh\n", func, line, r->len); | |
40 | pr_debug("%s:%d: lpar_addr %lxh\n", func, line, r->lpar_addr); | |
41 | } | |
42 | ||
43 | int ps3_mmio_region_create(struct ps3_mmio_region *r) | |
44 | { | |
45 | int result; | |
46 | ||
47 | result = lv1_map_device_mmio_region(r->did.bus_id, r->did.dev_id, | |
48 | r->bus_addr, r->len, r->page_size, &r->lpar_addr); | |
49 | ||
50 | if (result) { | |
51 | pr_debug("%s:%d: lv1_map_device_mmio_region failed: %s\n", | |
52 | __func__, __LINE__, ps3_result(result)); | |
43d80439 | 53 | r->lpar_addr = 0; |
a3d4d643 GL |
54 | } |
55 | ||
56 | dump_mmio_region(r); | |
57 | return result; | |
58 | } | |
59 | ||
60 | int ps3_free_mmio_region(struct ps3_mmio_region *r) | |
61 | { | |
62 | int result; | |
63 | ||
64 | result = lv1_unmap_device_mmio_region(r->did.bus_id, r->did.dev_id, | |
43d80439 | 65 | r->lpar_addr); |
a3d4d643 GL |
66 | |
67 | if (result) | |
68 | pr_debug("%s:%d: lv1_unmap_device_mmio_region failed: %s\n", | |
69 | __func__, __LINE__, ps3_result(result)); | |
70 | ||
43d80439 | 71 | r->lpar_addr = 0; |
a3d4d643 GL |
72 | return result; |
73 | } | |
74 | ||
75 | static int ps3_system_bus_match(struct device *_dev, | |
76 | struct device_driver *_drv) | |
77 | { | |
78 | int result; | |
79 | struct ps3_system_bus_driver *drv = to_ps3_system_bus_driver(_drv); | |
80 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
81 | ||
82 | result = dev->match_id == drv->match_id; | |
83 | ||
84 | pr_info("%s:%d: dev=%u(%s), drv=%u(%s): %s\n", __func__, __LINE__, | |
85 | dev->match_id, dev->core.bus_id, drv->match_id, drv->core.name, | |
86 | (result ? "match" : "miss")); | |
87 | return result; | |
88 | } | |
89 | ||
90 | static int ps3_system_bus_probe(struct device *_dev) | |
91 | { | |
92 | int result; | |
93 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
94 | struct ps3_system_bus_driver *drv = | |
95 | to_ps3_system_bus_driver(_dev->driver); | |
96 | ||
97 | result = lv1_open_device(dev->did.bus_id, dev->did.dev_id, 0); | |
98 | ||
99 | if (result) { | |
100 | pr_debug("%s:%d: lv1_open_device failed (%d)\n", | |
101 | __func__, __LINE__, result); | |
102 | result = -EACCES; | |
103 | goto clean_none; | |
104 | } | |
105 | ||
106 | if (dev->d_region->did.bus_id) { | |
107 | result = ps3_dma_region_create(dev->d_region); | |
108 | ||
109 | if (result) { | |
110 | pr_debug("%s:%d: ps3_dma_region_create failed (%d)\n", | |
111 | __func__, __LINE__, result); | |
112 | BUG_ON("check region type"); | |
113 | result = -EINVAL; | |
114 | goto clean_device; | |
115 | } | |
116 | } | |
117 | ||
118 | BUG_ON(!drv); | |
119 | ||
120 | if (drv->probe) | |
121 | result = drv->probe(dev); | |
122 | else | |
123 | pr_info("%s:%d: %s no probe method\n", __func__, __LINE__, | |
124 | dev->core.bus_id); | |
125 | ||
126 | if (result) { | |
127 | pr_debug("%s:%d: drv->probe failed\n", __func__, __LINE__); | |
128 | goto clean_dma; | |
129 | } | |
130 | ||
131 | return result; | |
132 | ||
133 | clean_dma: | |
134 | ps3_dma_region_free(dev->d_region); | |
135 | clean_device: | |
136 | lv1_close_device(dev->did.bus_id, dev->did.dev_id); | |
137 | clean_none: | |
138 | return result; | |
139 | } | |
140 | ||
141 | static int ps3_system_bus_remove(struct device *_dev) | |
142 | { | |
143 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
144 | struct ps3_system_bus_driver *drv = | |
145 | to_ps3_system_bus_driver(_dev->driver); | |
146 | ||
147 | if (drv->remove) | |
148 | drv->remove(dev); | |
149 | else | |
150 | pr_info("%s:%d: %s no remove method\n", __func__, __LINE__, | |
151 | dev->core.bus_id); | |
152 | ||
153 | ps3_dma_region_free(dev->d_region); | |
154 | ps3_free_mmio_region(dev->m_region); | |
155 | lv1_close_device(dev->did.bus_id, dev->did.dev_id); | |
156 | ||
157 | return 0; | |
158 | } | |
159 | ||
160 | struct bus_type ps3_system_bus_type = { | |
161 | .name = "ps3_system_bus", | |
162 | .match = ps3_system_bus_match, | |
163 | .probe = ps3_system_bus_probe, | |
164 | .remove = ps3_system_bus_remove, | |
165 | }; | |
166 | ||
167 | int __init ps3_system_bus_init(void) | |
168 | { | |
169 | int result; | |
170 | ||
e22ba7e3 AB |
171 | if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) |
172 | return 0; | |
173 | ||
a3d4d643 GL |
174 | result = bus_register(&ps3_system_bus_type); |
175 | BUG_ON(result); | |
176 | return result; | |
177 | } | |
178 | ||
179 | core_initcall(ps3_system_bus_init); | |
180 | ||
181 | /* Allocates a contiguous real buffer and creates mappings over it. | |
182 | * Returns the virtual address of the buffer and sets dma_handle | |
183 | * to the dma address (mapping) of the first page. | |
184 | */ | |
185 | ||
186 | static void * ps3_alloc_coherent(struct device *_dev, size_t size, | |
187 | dma_addr_t *dma_handle, gfp_t flag) | |
188 | { | |
189 | int result; | |
190 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
191 | unsigned long virt_addr; | |
192 | ||
193 | BUG_ON(!dev->d_region->bus_addr); | |
194 | ||
195 | flag &= ~(__GFP_DMA | __GFP_HIGHMEM); | |
196 | flag |= __GFP_ZERO; | |
197 | ||
198 | virt_addr = __get_free_pages(flag, get_order(size)); | |
199 | ||
200 | if (!virt_addr) { | |
201 | pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__); | |
202 | goto clean_none; | |
203 | } | |
204 | ||
205 | result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle); | |
206 | ||
207 | if (result) { | |
208 | pr_debug("%s:%d: ps3_dma_map failed (%d)\n", | |
209 | __func__, __LINE__, result); | |
210 | BUG_ON("check region type"); | |
211 | goto clean_alloc; | |
212 | } | |
213 | ||
214 | return (void*)virt_addr; | |
215 | ||
216 | clean_alloc: | |
217 | free_pages(virt_addr, get_order(size)); | |
218 | clean_none: | |
219 | dma_handle = NULL; | |
220 | return NULL; | |
221 | } | |
222 | ||
223 | static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr, | |
224 | dma_addr_t dma_handle) | |
225 | { | |
226 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
227 | ||
228 | ps3_dma_unmap(dev->d_region, dma_handle, size); | |
229 | free_pages((unsigned long)vaddr, get_order(size)); | |
230 | } | |
231 | ||
232 | /* Creates TCEs for a user provided buffer. The user buffer must be | |
233 | * contiguous real kernel storage (not vmalloc). The address of the buffer | |
234 | * passed here is the kernel (virtual) address of the buffer. The buffer | |
235 | * need not be page aligned, the dma_addr_t returned will point to the same | |
236 | * byte within the page as vaddr. | |
237 | */ | |
238 | ||
239 | static dma_addr_t ps3_map_single(struct device *_dev, void *ptr, size_t size, | |
240 | enum dma_data_direction direction) | |
241 | { | |
242 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
243 | int result; | |
244 | unsigned long bus_addr; | |
245 | ||
246 | result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size, | |
247 | &bus_addr); | |
248 | ||
249 | if (result) { | |
250 | pr_debug("%s:%d: ps3_dma_map failed (%d)\n", | |
251 | __func__, __LINE__, result); | |
252 | } | |
253 | ||
254 | return bus_addr; | |
255 | } | |
256 | ||
257 | static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr, | |
258 | size_t size, enum dma_data_direction direction) | |
259 | { | |
260 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
261 | int result; | |
262 | ||
263 | result = ps3_dma_unmap(dev->d_region, dma_addr, size); | |
264 | ||
265 | if (result) { | |
266 | pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n", | |
267 | __func__, __LINE__, result); | |
268 | } | |
269 | } | |
270 | ||
271 | static int ps3_map_sg(struct device *_dev, struct scatterlist *sg, int nents, | |
272 | enum dma_data_direction direction) | |
273 | { | |
274 | #if defined(CONFIG_PS3_DYNAMIC_DMA) | |
275 | BUG_ON("do"); | |
276 | #endif | |
277 | return 0; | |
278 | } | |
279 | ||
280 | static void ps3_unmap_sg(struct device *_dev, struct scatterlist *sg, | |
281 | int nents, enum dma_data_direction direction) | |
282 | { | |
283 | #if defined(CONFIG_PS3_DYNAMIC_DMA) | |
284 | BUG_ON("do"); | |
285 | #endif | |
286 | } | |
287 | ||
288 | static int ps3_dma_supported(struct device *_dev, u64 mask) | |
289 | { | |
290 | return 1; | |
291 | } | |
292 | ||
293 | static struct dma_mapping_ops ps3_dma_ops = { | |
294 | .alloc_coherent = ps3_alloc_coherent, | |
295 | .free_coherent = ps3_free_coherent, | |
296 | .map_single = ps3_map_single, | |
297 | .unmap_single = ps3_unmap_single, | |
298 | .map_sg = ps3_map_sg, | |
299 | .unmap_sg = ps3_unmap_sg, | |
300 | .dma_supported = ps3_dma_supported | |
301 | }; | |
302 | ||
303 | /** | |
304 | * ps3_system_bus_release_device - remove a device from the system bus | |
305 | */ | |
306 | ||
307 | static void ps3_system_bus_release_device(struct device *_dev) | |
308 | { | |
309 | struct ps3_system_bus_device *dev = to_ps3_system_bus_device(_dev); | |
310 | kfree(dev); | |
311 | } | |
312 | ||
313 | /** | |
314 | * ps3_system_bus_device_register - add a device to the system bus | |
315 | * | |
316 | * ps3_system_bus_device_register() expects the dev object to be allocated | |
317 | * dynamically by the caller. The system bus takes ownership of the dev | |
318 | * object and frees the object in ps3_system_bus_release_device(). | |
319 | */ | |
320 | ||
321 | int ps3_system_bus_device_register(struct ps3_system_bus_device *dev) | |
322 | { | |
323 | int result; | |
324 | static unsigned int dev_count = 1; | |
325 | ||
326 | dev->core.parent = NULL; | |
327 | dev->core.bus = &ps3_system_bus_type; | |
328 | dev->core.release = ps3_system_bus_release_device; | |
329 | ||
330 | dev->core.archdata.of_node = NULL; | |
331 | dev->core.archdata.dma_ops = &ps3_dma_ops; | |
332 | dev->core.archdata.numa_node = 0; | |
333 | ||
334 | snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "sb_%02x", | |
335 | dev_count++); | |
336 | ||
337 | pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id); | |
338 | ||
339 | result = device_register(&dev->core); | |
340 | return result; | |
341 | } | |
342 | ||
343 | EXPORT_SYMBOL_GPL(ps3_system_bus_device_register); | |
344 | ||
345 | int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv) | |
346 | { | |
347 | int result; | |
348 | ||
349 | drv->core.bus = &ps3_system_bus_type; | |
350 | ||
351 | result = driver_register(&drv->core); | |
352 | return result; | |
353 | } | |
354 | ||
355 | EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register); | |
356 | ||
357 | void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv) | |
358 | { | |
359 | driver_unregister(&drv->core); | |
360 | } | |
361 | ||
362 | EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister); |