📄 system-bus.c
字号:
if (drv->remove) result = drv->remove(dev); else dev_dbg(&dev->core, "%s:%d %s: no remove method\n", __func__, __LINE__, drv->core.name); pr_info(" <- %s:%d: %s\n", __func__, __LINE__, dev->core.bus_id); return result;}static void ps3_system_bus_shutdown(struct device *_dev){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); struct ps3_system_bus_driver *drv; BUG_ON(!dev); dev_dbg(&dev->core, " -> %s:%d: match_id %d\n", __func__, __LINE__, dev->match_id); if (!dev->core.driver) { dev_dbg(&dev->core, "%s:%d: no driver bound\n", __func__, __LINE__); return; } drv = ps3_system_bus_dev_to_system_bus_drv(dev); BUG_ON(!drv); dev_dbg(&dev->core, "%s:%d: %s -> %s\n", __func__, __LINE__, dev->core.bus_id, drv->core.name); if (drv->shutdown) drv->shutdown(dev); else if (drv->remove) { dev_dbg(&dev->core, "%s:%d %s: no shutdown, calling remove\n", __func__, __LINE__, drv->core.name); drv->remove(dev); } else { dev_dbg(&dev->core, "%s:%d %s: no shutdown method\n", __func__, __LINE__, drv->core.name); BUG(); } dev_dbg(&dev->core, " <- %s:%d\n", __func__, __LINE__);}static int ps3_system_bus_uevent(struct device *_dev, struct kobj_uevent_env *env){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); if (add_uevent_var(env, "MODALIAS=ps3:%d", dev->match_id)) return -ENOMEM; return 0;}static ssize_t modalias_show(struct device *_dev, struct device_attribute *a, char *buf){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); int len = snprintf(buf, PAGE_SIZE, "ps3:%d\n", dev->match_id); return (len >= PAGE_SIZE) ? (PAGE_SIZE - 1) : len;}static struct device_attribute ps3_system_bus_dev_attrs[] = { __ATTR_RO(modalias), __ATTR_NULL,};struct bus_type ps3_system_bus_type = { .name = "ps3_system_bus", .match = ps3_system_bus_match, .uevent = ps3_system_bus_uevent, .probe = ps3_system_bus_probe, .remove = ps3_system_bus_remove, .shutdown = ps3_system_bus_shutdown, .dev_attrs = ps3_system_bus_dev_attrs,};static int __init ps3_system_bus_init(void){ int result; if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) return -ENODEV; pr_debug(" -> %s:%d\n", __func__, __LINE__); mutex_init(&usage_hack.mutex); result = device_register(&ps3_system_bus); BUG_ON(result); result = bus_register(&ps3_system_bus_type); BUG_ON(result); pr_debug(" <- %s:%d\n", __func__, __LINE__); return result;}core_initcall(ps3_system_bus_init);/* Allocates a contiguous real buffer and creates mappings over it. * Returns the virtual address of the buffer and sets dma_handle * to the dma address (mapping) of the first page. */static void * ps3_alloc_coherent(struct device *_dev, size_t size, dma_addr_t *dma_handle, gfp_t flag){ int result; struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); unsigned long virt_addr; flag &= ~(__GFP_DMA | __GFP_HIGHMEM); flag |= __GFP_ZERO; virt_addr = __get_free_pages(flag, get_order(size)); if (!virt_addr) { pr_debug("%s:%d: get_free_pages failed\n", __func__, __LINE__); goto clean_none; } result = ps3_dma_map(dev->d_region, virt_addr, size, dma_handle, IOPTE_PP_W | IOPTE_PP_R | IOPTE_SO_RW | IOPTE_M); if (result) { pr_debug("%s:%d: ps3_dma_map failed (%d)\n", __func__, __LINE__, result); BUG_ON("check region type"); goto clean_alloc; } return (void*)virt_addr;clean_alloc: free_pages(virt_addr, get_order(size));clean_none: dma_handle = NULL; return NULL;}static void ps3_free_coherent(struct device *_dev, size_t size, void *vaddr, dma_addr_t dma_handle){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); ps3_dma_unmap(dev->d_region, dma_handle, size); free_pages((unsigned long)vaddr, get_order(size));}/* Creates TCEs for a user provided buffer. The user buffer must be * contiguous real kernel storage (not vmalloc). The address of the buffer * passed here is the kernel (virtual) address of the buffer. The buffer * need not be page aligned, the dma_addr_t returned will point to the same * byte within the page as vaddr. */static dma_addr_t ps3_sb_map_single(struct device *_dev, void *ptr, size_t size, enum dma_data_direction direction){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); int result; unsigned long bus_addr; result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size, &bus_addr, IOPTE_PP_R | IOPTE_PP_W | IOPTE_SO_RW | IOPTE_M); if (result) { pr_debug("%s:%d: ps3_dma_map failed (%d)\n", __func__, __LINE__, result); } return bus_addr;}static dma_addr_t ps3_ioc0_map_single(struct device *_dev, void *ptr, size_t size, enum dma_data_direction direction){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); int result; unsigned long bus_addr; u64 iopte_flag; iopte_flag = IOPTE_M; switch (direction) { case DMA_BIDIRECTIONAL: iopte_flag |= IOPTE_PP_R | IOPTE_PP_W | IOPTE_SO_RW; break; case DMA_TO_DEVICE: iopte_flag |= IOPTE_PP_R | IOPTE_SO_R; break; case DMA_FROM_DEVICE: iopte_flag |= IOPTE_PP_W | IOPTE_SO_RW; break; default: /* not happned */ BUG(); }; result = ps3_dma_map(dev->d_region, (unsigned long)ptr, size, &bus_addr, iopte_flag); if (result) { pr_debug("%s:%d: ps3_dma_map failed (%d)\n", __func__, __LINE__, result); } return bus_addr;}static void ps3_unmap_single(struct device *_dev, dma_addr_t dma_addr, size_t size, enum dma_data_direction direction){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); int result; result = ps3_dma_unmap(dev->d_region, dma_addr, size); if (result) { pr_debug("%s:%d: ps3_dma_unmap failed (%d)\n", __func__, __LINE__, result); }}static int ps3_sb_map_sg(struct device *_dev, struct scatterlist *sgl, int nents, enum dma_data_direction direction){#if defined(CONFIG_PS3_DYNAMIC_DMA) BUG_ON("do"); return -EPERM;#else struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); struct scatterlist *sg; int i; for_each_sg(sgl, sg, nents, i) { int result = ps3_dma_map(dev->d_region, sg_phys(sg), sg->length, &sg->dma_address, 0); if (result) { pr_debug("%s:%d: ps3_dma_map failed (%d)\n", __func__, __LINE__, result); return -EINVAL; } sg->dma_length = sg->length; } return nents;#endif}static int ps3_ioc0_map_sg(struct device *_dev, struct scatterlist *sg, int nents, enum dma_data_direction direction){ BUG(); return 0;}static void ps3_sb_unmap_sg(struct device *_dev, struct scatterlist *sg, int nents, enum dma_data_direction direction){#if defined(CONFIG_PS3_DYNAMIC_DMA) BUG_ON("do");#endif}static void ps3_ioc0_unmap_sg(struct device *_dev, struct scatterlist *sg, int nents, enum dma_data_direction direction){ BUG();}static int ps3_dma_supported(struct device *_dev, u64 mask){ return mask >= DMA_32BIT_MASK;}static struct dma_mapping_ops ps3_sb_dma_ops = { .alloc_coherent = ps3_alloc_coherent, .free_coherent = ps3_free_coherent, .map_single = ps3_sb_map_single, .unmap_single = ps3_unmap_single, .map_sg = ps3_sb_map_sg, .unmap_sg = ps3_sb_unmap_sg, .dma_supported = ps3_dma_supported};static struct dma_mapping_ops ps3_ioc0_dma_ops = { .alloc_coherent = ps3_alloc_coherent, .free_coherent = ps3_free_coherent, .map_single = ps3_ioc0_map_single, .unmap_single = ps3_unmap_single, .map_sg = ps3_ioc0_map_sg, .unmap_sg = ps3_ioc0_unmap_sg, .dma_supported = ps3_dma_supported};/** * ps3_system_bus_release_device - remove a device from the system bus */static void ps3_system_bus_release_device(struct device *_dev){ struct ps3_system_bus_device *dev = ps3_dev_to_system_bus_dev(_dev); kfree(dev);}/** * ps3_system_bus_device_register - add a device to the system bus * * ps3_system_bus_device_register() expects the dev object to be allocated * dynamically by the caller. The system bus takes ownership of the dev * object and frees the object in ps3_system_bus_release_device(). */int ps3_system_bus_device_register(struct ps3_system_bus_device *dev){ int result; static unsigned int dev_ioc0_count; static unsigned int dev_sb_count; static unsigned int dev_vuart_count; if (!dev->core.parent) dev->core.parent = &ps3_system_bus; dev->core.bus = &ps3_system_bus_type; dev->core.release = ps3_system_bus_release_device; switch (dev->dev_type) { case PS3_DEVICE_TYPE_IOC0: dev->core.archdata.dma_ops = &ps3_ioc0_dma_ops; snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "ioc0_%02x", ++dev_ioc0_count); break; case PS3_DEVICE_TYPE_SB: dev->core.archdata.dma_ops = &ps3_sb_dma_ops; snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "sb_%02x", ++dev_sb_count); break; case PS3_DEVICE_TYPE_VUART: snprintf(dev->core.bus_id, sizeof(dev->core.bus_id), "vuart_%02x", ++dev_vuart_count); break; default: BUG(); }; dev->core.archdata.of_node = NULL; dev->core.archdata.numa_node = 0; pr_debug("%s:%d add %s\n", __func__, __LINE__, dev->core.bus_id); result = device_register(&dev->core); return result;}EXPORT_SYMBOL_GPL(ps3_system_bus_device_register);int ps3_system_bus_driver_register(struct ps3_system_bus_driver *drv){ int result; pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name); if (!firmware_has_feature(FW_FEATURE_PS3_LV1)) return -ENODEV; drv->core.bus = &ps3_system_bus_type; result = driver_register(&drv->core); pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name); return result;}EXPORT_SYMBOL_GPL(ps3_system_bus_driver_register);void ps3_system_bus_driver_unregister(struct ps3_system_bus_driver *drv){ pr_debug(" -> %s:%d: %s\n", __func__, __LINE__, drv->core.name); driver_unregister(&drv->core); pr_debug(" <- %s:%d: %s\n", __func__, __LINE__, drv->core.name);}EXPORT_SYMBOL_GPL(ps3_system_bus_driver_unregister);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -