osl.c
来自「linux 内核源代码」· C语言 代码 · 共 1,313 行 · 第 1/3 页
C
1,313 行
*(u32 *) value = inl(port); } else { BUG(); } return AE_OK;}EXPORT_SYMBOL(acpi_os_read_port);acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width){ if (width <= 8) { outb(value, port); } else if (width <= 16) { outw(value, port); } else if (width <= 32) { outl(value, port); } else { BUG(); } return AE_OK;}EXPORT_SYMBOL(acpi_os_write_port);acpi_statusacpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width){ u32 dummy; void __iomem *virt_addr; virt_addr = ioremap(phys_addr, width); if (!value) value = &dummy; switch (width) { case 8: *(u8 *) value = readb(virt_addr); break; case 16: *(u16 *) value = readw(virt_addr); break; case 32: *(u32 *) value = readl(virt_addr); break; default: BUG(); } iounmap(virt_addr); return AE_OK;}acpi_statusacpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width){ void __iomem *virt_addr; virt_addr = ioremap(phys_addr, width); switch (width) { case 8: writeb(value, virt_addr); break; case 16: writew(value, virt_addr); break; case 32: writel(value, virt_addr); break; default: BUG(); } iounmap(virt_addr); return AE_OK;}acpi_statusacpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg, void *value, u32 width){ int result, size; if (!value) return AE_BAD_PARAMETER; switch (width) { case 8: size = 1; break; case 16: size = 2; break; case 32: size = 4; break; default: return AE_ERROR; } BUG_ON(!raw_pci_ops); result = raw_pci_ops->read(pci_id->segment, pci_id->bus, PCI_DEVFN(pci_id->device, pci_id->function), reg, size, value); return (result ? AE_ERROR : AE_OK);}EXPORT_SYMBOL(acpi_os_read_pci_configuration);acpi_statusacpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg, acpi_integer value, u32 width){ int result, size; switch (width) { case 8: size = 1; break; case 16: size = 2; break; case 32: size = 4; break; default: return AE_ERROR; } BUG_ON(!raw_pci_ops); result = raw_pci_ops->write(pci_id->segment, pci_id->bus, PCI_DEVFN(pci_id->device, pci_id->function), reg, size, value); return (result ? AE_ERROR : AE_OK);}/* TODO: Change code to take advantage of driver model more */static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */ acpi_handle chandle, /* current node */ struct acpi_pci_id **id, int *is_bridge, u8 * bus_number){ acpi_handle handle; struct acpi_pci_id *pci_id = *id; acpi_status status; unsigned long temp; acpi_object_type type; u8 tu8; acpi_get_parent(chandle, &handle); if (handle != rhandle) { acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge, bus_number); status = acpi_get_type(handle, &type); if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE)) return; status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL, &temp); if (ACPI_SUCCESS(status)) { pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp)); pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp)); if (*is_bridge) pci_id->bus = *bus_number; /* any nicer way to get bus number of bridge ? */ status = acpi_os_read_pci_configuration(pci_id, 0x0e, &tu8, 8); if (ACPI_SUCCESS(status) && ((tu8 & 0x7f) == 1 || (tu8 & 0x7f) == 2)) { status = acpi_os_read_pci_configuration(pci_id, 0x18, &tu8, 8); if (!ACPI_SUCCESS(status)) { /* Certainly broken... FIX ME */ return; } *is_bridge = 1; pci_id->bus = tu8; status = acpi_os_read_pci_configuration(pci_id, 0x19, &tu8, 8); if (ACPI_SUCCESS(status)) { *bus_number = tu8; } } else *is_bridge = 0; } }}void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */ acpi_handle chandle, /* current node */ struct acpi_pci_id **id){ int is_bridge = 1; u8 bus_number = (*id)->bus; acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);}static void acpi_os_execute_deferred(struct work_struct *work){ struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work); if (!dpc) { printk(KERN_ERR PREFIX "Invalid (NULL) context\n"); return; } dpc->function(dpc->context); kfree(dpc); /* Yield cpu to notify thread */ cond_resched(); return;}static void acpi_os_execute_notify(struct work_struct *work){ struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work); if (!dpc) { printk(KERN_ERR PREFIX "Invalid (NULL) context\n"); return; } dpc->function(dpc->context); kfree(dpc); return;}/******************************************************************************* * * FUNCTION: acpi_os_execute * * PARAMETERS: Type - Type of the callback * Function - Function to be executed * Context - Function parameters * * RETURN: Status * * DESCRIPTION: Depending on type, either queues function for deferred execution or * immediately executes function on a separate thread. * ******************************************************************************/acpi_status acpi_os_execute(acpi_execute_type type, acpi_osd_exec_callback function, void *context){ acpi_status status = AE_OK; struct acpi_os_dpc *dpc; ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Scheduling function [%p(%p)] for deferred execution.\n", function, context)); if (!function) return AE_BAD_PARAMETER; /* * Allocate/initialize DPC structure. Note that this memory will be * freed by the callee. The kernel handles the work_struct list in a * way that allows us to also free its memory inside the callee. * Because we may want to schedule several tasks with different * parameters we can't use the approach some kernel code uses of * having a static work_struct. */ dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC); if (!dpc) return_ACPI_STATUS(AE_NO_MEMORY); dpc->function = function; dpc->context = context; if (type == OSL_NOTIFY_HANDLER) { INIT_WORK(&dpc->work, acpi_os_execute_notify); if (!queue_work(kacpi_notify_wq, &dpc->work)) { status = AE_ERROR; kfree(dpc); } } else { INIT_WORK(&dpc->work, acpi_os_execute_deferred); if (!queue_work(kacpid_wq, &dpc->work)) { ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Call to queue_work() failed.\n")); status = AE_ERROR; kfree(dpc); } } return_ACPI_STATUS(status);}EXPORT_SYMBOL(acpi_os_execute);void acpi_os_wait_events_complete(void *context){ flush_workqueue(kacpid_wq);}EXPORT_SYMBOL(acpi_os_wait_events_complete);/* * Allocate the memory for a spinlock and initialize it. */acpi_status acpi_os_create_lock(acpi_spinlock * handle){ spin_lock_init(*handle); return AE_OK;}/* * Deallocate the memory for a spinlock. */void acpi_os_delete_lock(acpi_spinlock handle){ return;}acpi_statusacpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle){ struct semaphore *sem = NULL; sem = acpi_os_allocate(sizeof(struct semaphore)); if (!sem) return AE_NO_MEMORY; memset(sem, 0, sizeof(struct semaphore)); sema_init(sem, initial_units); *handle = (acpi_handle *) sem; ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n", *handle, initial_units)); return AE_OK;}EXPORT_SYMBOL(acpi_os_create_semaphore);/* * TODO: A better way to delete semaphores? Linux doesn't have a * 'delete_semaphore()' function -- may result in an invalid * pointer dereference for non-synchronized consumers. Should * we at least check for blocked threads and signal/cancel them? */acpi_status acpi_os_delete_semaphore(acpi_handle handle){ struct semaphore *sem = (struct semaphore *)handle; if (!sem) return AE_BAD_PARAMETER; ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle)); kfree(sem); sem = NULL; return AE_OK;}EXPORT_SYMBOL(acpi_os_delete_semaphore);/* * TODO: The kernel doesn't have a 'down_timeout' function -- had to * improvise. The process is to sleep for one scheduler quantum * until the semaphore becomes available. Downside is that this * may result in starvation for timeout-based waits when there's * lots of semaphore activity. * * TODO: Support for units > 1? */acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout){ acpi_status status = AE_OK; struct semaphore *sem = (struct semaphore *)handle; int ret = 0; if (!sem || (units < 1)) return AE_BAD_PARAMETER; if (units > 1) return AE_SUPPORT; ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n", handle, units, timeout)); /* * This can be called during resume with interrupts off. * Like boot-time, we should be single threaded and will * always get the lock if we try -- timeout or not. * If this doesn't succeed, then we will oops courtesy of * might_sleep() in down(). */ if (!down_trylock(sem)) return AE_OK; switch (timeout) { /* * No Wait: * -------- * A zero timeout value indicates that we shouldn't wait - just * acquire the semaphore if available otherwise return AE_TIME * (a.k.a. 'would block'). */ case 0: if (down_trylock(sem)) status = AE_TIME; break; /* * Wait Indefinitely: * ------------------ */ case ACPI_WAIT_FOREVER: down(sem);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?