osl.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. /*
  2. * acpi_osl.c - OS-dependent functions ($Revision: 83 $)
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (c) 2008 Intel Corporation
  8. * Author: Matthew Wilcox <willy@linux.intel.com>
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/mm.h>
  33. #include <linux/pci.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/kmod.h>
  36. #include <linux/delay.h>
  37. #include <linux/workqueue.h>
  38. #include <linux/nmi.h>
  39. #include <linux/acpi.h>
  40. #include <linux/efi.h>
  41. #include <linux/ioport.h>
  42. #include <linux/list.h>
  43. #include <linux/jiffies.h>
  44. #include <linux/semaphore.h>
  45. #include <asm/io.h>
  46. #include <asm/uaccess.h>
  47. #include <acpi/acpi.h>
  48. #include <acpi/acpi_bus.h>
  49. #include <acpi/processor.h>
  50. #define _COMPONENT ACPI_OS_SERVICES
  51. ACPI_MODULE_NAME("osl");
  52. #define PREFIX "ACPI: "
  53. struct acpi_os_dpc {
  54. acpi_osd_exec_callback function;
  55. void *context;
  56. struct work_struct work;
  57. int wait;
  58. };
  59. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  60. #include CONFIG_ACPI_CUSTOM_DSDT_FILE
  61. #endif
  62. #ifdef ENABLE_DEBUGGER
  63. #include <linux/kdb.h>
  64. /* stuff for debugger support */
  65. int acpi_in_debugger;
  66. EXPORT_SYMBOL(acpi_in_debugger);
  67. extern char line_buf[80];
  68. #endif /*ENABLE_DEBUGGER */
  69. static unsigned int acpi_irq_irq;
  70. static acpi_osd_handler acpi_irq_handler;
  71. static void *acpi_irq_context;
  72. static struct workqueue_struct *kacpid_wq;
  73. static struct workqueue_struct *kacpi_notify_wq;
  74. static struct workqueue_struct *kacpi_hotplug_wq;
  75. struct acpi_res_list {
  76. resource_size_t start;
  77. resource_size_t end;
  78. acpi_adr_space_type resource_type; /* IO port, System memory, ...*/
  79. char name[5]; /* only can have a length of 4 chars, make use of this
  80. one instead of res->name, no need to kalloc then */
  81. struct list_head resource_list;
  82. int count;
  83. };
  84. static LIST_HEAD(resource_list_head);
  85. static DEFINE_SPINLOCK(acpi_res_lock);
  86. /*
  87. * This list of permanent mappings is for memory that may be accessed from
  88. * interrupt context, where we can't do the ioremap().
  89. */
  90. struct acpi_ioremap {
  91. struct list_head list;
  92. void __iomem *virt;
  93. acpi_physical_address phys;
  94. acpi_size size;
  95. };
  96. static LIST_HEAD(acpi_ioremaps);
  97. static DEFINE_SPINLOCK(acpi_ioremap_lock);
  98. #define OSI_STRING_LENGTH_MAX 64 /* arbitrary */
  99. static char osi_additional_string[OSI_STRING_LENGTH_MAX];
  100. /*
  101. * The story of _OSI(Linux)
  102. *
  103. * From pre-history through Linux-2.6.22,
  104. * Linux responded TRUE upon a BIOS OSI(Linux) query.
  105. *
  106. * Unfortunately, reference BIOS writers got wind of this
  107. * and put OSI(Linux) in their example code, quickly exposing
  108. * this string as ill-conceived and opening the door to
  109. * an un-bounded number of BIOS incompatibilities.
  110. *
  111. * For example, OSI(Linux) was used on resume to re-POST a
  112. * video card on one system, because Linux at that time
  113. * could not do a speedy restore in its native driver.
  114. * But then upon gaining quick native restore capability,
  115. * Linux has no way to tell the BIOS to skip the time-consuming
  116. * POST -- putting Linux at a permanent performance disadvantage.
  117. * On another system, the BIOS writer used OSI(Linux)
  118. * to infer native OS support for IPMI! On other systems,
  119. * OSI(Linux) simply got in the way of Linux claiming to
  120. * be compatible with other operating systems, exposing
  121. * BIOS issues such as skipped device initialization.
  122. *
  123. * So "Linux" turned out to be a really poor chose of
  124. * OSI string, and from Linux-2.6.23 onward we respond FALSE.
  125. *
  126. * BIOS writers should NOT query _OSI(Linux) on future systems.
  127. * Linux will complain on the console when it sees it, and return FALSE.
  128. * To get Linux to return TRUE for your system will require
  129. * a kernel source update to add a DMI entry,
  130. * or boot with "acpi_osi=Linux"
  131. */
  132. static struct osi_linux {
  133. unsigned int enable:1;
  134. unsigned int dmi:1;
  135. unsigned int cmdline:1;
  136. unsigned int known:1;
  137. } osi_linux = { 0, 0, 0, 0};
  138. static void __init acpi_request_region (struct acpi_generic_address *addr,
  139. unsigned int length, char *desc)
  140. {
  141. if (!addr->address || !length)
  142. return;
  143. /* Resources are never freed */
  144. if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  145. request_region(addr->address, length, desc);
  146. else if (addr->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY)
  147. request_mem_region(addr->address, length, desc);
  148. }
  149. static int __init acpi_reserve_resources(void)
  150. {
  151. acpi_request_region(&acpi_gbl_FADT.xpm1a_event_block, acpi_gbl_FADT.pm1_event_length,
  152. "ACPI PM1a_EVT_BLK");
  153. acpi_request_region(&acpi_gbl_FADT.xpm1b_event_block, acpi_gbl_FADT.pm1_event_length,
  154. "ACPI PM1b_EVT_BLK");
  155. acpi_request_region(&acpi_gbl_FADT.xpm1a_control_block, acpi_gbl_FADT.pm1_control_length,
  156. "ACPI PM1a_CNT_BLK");
  157. acpi_request_region(&acpi_gbl_FADT.xpm1b_control_block, acpi_gbl_FADT.pm1_control_length,
  158. "ACPI PM1b_CNT_BLK");
  159. if (acpi_gbl_FADT.pm_timer_length == 4)
  160. acpi_request_region(&acpi_gbl_FADT.xpm_timer_block, 4, "ACPI PM_TMR");
  161. acpi_request_region(&acpi_gbl_FADT.xpm2_control_block, acpi_gbl_FADT.pm2_control_length,
  162. "ACPI PM2_CNT_BLK");
  163. /* Length of GPE blocks must be a non-negative multiple of 2 */
  164. if (!(acpi_gbl_FADT.gpe0_block_length & 0x1))
  165. acpi_request_region(&acpi_gbl_FADT.xgpe0_block,
  166. acpi_gbl_FADT.gpe0_block_length, "ACPI GPE0_BLK");
  167. if (!(acpi_gbl_FADT.gpe1_block_length & 0x1))
  168. acpi_request_region(&acpi_gbl_FADT.xgpe1_block,
  169. acpi_gbl_FADT.gpe1_block_length, "ACPI GPE1_BLK");
  170. return 0;
  171. }
  172. device_initcall(acpi_reserve_resources);
  173. void acpi_os_printf(const char *fmt, ...)
  174. {
  175. va_list args;
  176. va_start(args, fmt);
  177. acpi_os_vprintf(fmt, args);
  178. va_end(args);
  179. }
  180. void acpi_os_vprintf(const char *fmt, va_list args)
  181. {
  182. static char buffer[512];
  183. vsprintf(buffer, fmt, args);
  184. #ifdef ENABLE_DEBUGGER
  185. if (acpi_in_debugger) {
  186. kdb_printf("%s", buffer);
  187. } else {
  188. printk(KERN_CONT "%s", buffer);
  189. }
  190. #else
  191. printk(KERN_CONT "%s", buffer);
  192. #endif
  193. }
  194. acpi_physical_address __init acpi_os_get_root_pointer(void)
  195. {
  196. if (efi_enabled) {
  197. if (efi.acpi20 != EFI_INVALID_TABLE_ADDR)
  198. return efi.acpi20;
  199. else if (efi.acpi != EFI_INVALID_TABLE_ADDR)
  200. return efi.acpi;
  201. else {
  202. printk(KERN_ERR PREFIX
  203. "System description tables not found\n");
  204. return 0;
  205. }
  206. } else {
  207. acpi_physical_address pa = 0;
  208. acpi_find_root_pointer(&pa);
  209. return pa;
  210. }
  211. }
  212. /* Must be called with 'acpi_ioremap_lock' lock held. */
  213. static void __iomem *
  214. acpi_map_vaddr_lookup(acpi_physical_address phys, acpi_size size)
  215. {
  216. struct acpi_ioremap *map;
  217. list_for_each_entry(map, &acpi_ioremaps, list)
  218. if (map->phys <= phys &&
  219. phys + size <= map->phys + map->size)
  220. return map->virt + (phys - map->phys);
  221. return NULL;
  222. }
  223. /* Must be called with 'acpi_ioremap_lock' lock held. */
  224. static struct acpi_ioremap *
  225. acpi_map_lookup_virt(void __iomem *virt, acpi_size size)
  226. {
  227. struct acpi_ioremap *map;
  228. list_for_each_entry(map, &acpi_ioremaps, list)
  229. if (map->virt == virt && map->size == size)
  230. return map;
  231. return NULL;
  232. }
  233. void __iomem *__init_refok
  234. acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
  235. {
  236. struct acpi_ioremap *map;
  237. unsigned long flags;
  238. void __iomem *virt;
  239. if (phys > ULONG_MAX) {
  240. printk(KERN_ERR PREFIX "Cannot map memory that high\n");
  241. return NULL;
  242. }
  243. if (!acpi_gbl_permanent_mmap)
  244. return __acpi_map_table((unsigned long)phys, size);
  245. map = kzalloc(sizeof(*map), GFP_KERNEL);
  246. if (!map)
  247. return NULL;
  248. virt = ioremap(phys, size);
  249. if (!virt) {
  250. kfree(map);
  251. return NULL;
  252. }
  253. INIT_LIST_HEAD(&map->list);
  254. map->virt = virt;
  255. map->phys = phys;
  256. map->size = size;
  257. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  258. list_add_tail(&map->list, &acpi_ioremaps);
  259. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  260. return virt;
  261. }
  262. EXPORT_SYMBOL_GPL(acpi_os_map_memory);
  263. void __ref acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  264. {
  265. struct acpi_ioremap *map;
  266. unsigned long flags;
  267. if (!acpi_gbl_permanent_mmap) {
  268. __acpi_unmap_table(virt, size);
  269. return;
  270. }
  271. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  272. map = acpi_map_lookup_virt(virt, size);
  273. if (!map) {
  274. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  275. printk(KERN_ERR PREFIX "%s: bad address %p\n", __func__, virt);
  276. dump_stack();
  277. return;
  278. }
  279. list_del(&map->list);
  280. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  281. iounmap(map->virt);
  282. kfree(map);
  283. }
  284. EXPORT_SYMBOL_GPL(acpi_os_unmap_memory);
  285. void __init early_acpi_os_unmap_memory(void __iomem *virt, acpi_size size)
  286. {
  287. if (!acpi_gbl_permanent_mmap)
  288. __acpi_unmap_table(virt, size);
  289. }
  290. int acpi_os_map_generic_address(struct acpi_generic_address *addr)
  291. {
  292. void __iomem *virt;
  293. if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  294. return 0;
  295. if (!addr->address || !addr->bit_width)
  296. return -EINVAL;
  297. virt = acpi_os_map_memory(addr->address, addr->bit_width / 8);
  298. if (!virt)
  299. return -EIO;
  300. return 0;
  301. }
  302. EXPORT_SYMBOL_GPL(acpi_os_map_generic_address);
  303. void acpi_os_unmap_generic_address(struct acpi_generic_address *addr)
  304. {
  305. void __iomem *virt;
  306. unsigned long flags;
  307. acpi_size size = addr->bit_width / 8;
  308. if (addr->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
  309. return;
  310. if (!addr->address || !addr->bit_width)
  311. return;
  312. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  313. virt = acpi_map_vaddr_lookup(addr->address, size);
  314. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  315. acpi_os_unmap_memory(virt, size);
  316. }
  317. EXPORT_SYMBOL_GPL(acpi_os_unmap_generic_address);
  318. #ifdef ACPI_FUTURE_USAGE
  319. acpi_status
  320. acpi_os_get_physical_address(void *virt, acpi_physical_address * phys)
  321. {
  322. if (!phys || !virt)
  323. return AE_BAD_PARAMETER;
  324. *phys = virt_to_phys(virt);
  325. return AE_OK;
  326. }
  327. #endif
  328. #define ACPI_MAX_OVERRIDE_LEN 100
  329. static char acpi_os_name[ACPI_MAX_OVERRIDE_LEN];
  330. acpi_status
  331. acpi_os_predefined_override(const struct acpi_predefined_names *init_val,
  332. acpi_string * new_val)
  333. {
  334. if (!init_val || !new_val)
  335. return AE_BAD_PARAMETER;
  336. *new_val = NULL;
  337. if (!memcmp(init_val->name, "_OS_", 4) && strlen(acpi_os_name)) {
  338. printk(KERN_INFO PREFIX "Overriding _OS definition to '%s'\n",
  339. acpi_os_name);
  340. *new_val = acpi_os_name;
  341. }
  342. return AE_OK;
  343. }
  344. acpi_status
  345. acpi_os_table_override(struct acpi_table_header * existing_table,
  346. struct acpi_table_header ** new_table)
  347. {
  348. if (!existing_table || !new_table)
  349. return AE_BAD_PARAMETER;
  350. *new_table = NULL;
  351. #ifdef CONFIG_ACPI_CUSTOM_DSDT
  352. if (strncmp(existing_table->signature, "DSDT", 4) == 0)
  353. *new_table = (struct acpi_table_header *)AmlCode;
  354. #endif
  355. if (*new_table != NULL) {
  356. printk(KERN_WARNING PREFIX "Override [%4.4s-%8.8s], "
  357. "this is unsafe: tainting kernel\n",
  358. existing_table->signature,
  359. existing_table->oem_table_id);
  360. add_taint(TAINT_OVERRIDDEN_ACPI_TABLE);
  361. }
  362. return AE_OK;
  363. }
  364. static irqreturn_t acpi_irq(int irq, void *dev_id)
  365. {
  366. u32 handled;
  367. handled = (*acpi_irq_handler) (acpi_irq_context);
  368. if (handled) {
  369. acpi_irq_handled++;
  370. return IRQ_HANDLED;
  371. } else {
  372. acpi_irq_not_handled++;
  373. return IRQ_NONE;
  374. }
  375. }
  376. acpi_status
  377. acpi_os_install_interrupt_handler(u32 gsi, acpi_osd_handler handler,
  378. void *context)
  379. {
  380. unsigned int irq;
  381. acpi_irq_stats_init();
  382. /*
  383. * Ignore the GSI from the core, and use the value in our copy of the
  384. * FADT. It may not be the same if an interrupt source override exists
  385. * for the SCI.
  386. */
  387. gsi = acpi_gbl_FADT.sci_interrupt;
  388. if (acpi_gsi_to_irq(gsi, &irq) < 0) {
  389. printk(KERN_ERR PREFIX "SCI (ACPI GSI %d) not registered\n",
  390. gsi);
  391. return AE_OK;
  392. }
  393. acpi_irq_handler = handler;
  394. acpi_irq_context = context;
  395. if (request_irq(irq, acpi_irq, IRQF_SHARED, "acpi", acpi_irq)) {
  396. printk(KERN_ERR PREFIX "SCI (IRQ%d) allocation failed\n", irq);
  397. return AE_NOT_ACQUIRED;
  398. }
  399. acpi_irq_irq = irq;
  400. return AE_OK;
  401. }
  402. acpi_status acpi_os_remove_interrupt_handler(u32 irq, acpi_osd_handler handler)
  403. {
  404. if (irq) {
  405. free_irq(irq, acpi_irq);
  406. acpi_irq_handler = NULL;
  407. acpi_irq_irq = 0;
  408. }
  409. return AE_OK;
  410. }
  411. /*
  412. * Running in interpreter thread context, safe to sleep
  413. */
  414. void acpi_os_sleep(u64 ms)
  415. {
  416. schedule_timeout_interruptible(msecs_to_jiffies(ms));
  417. }
  418. void acpi_os_stall(u32 us)
  419. {
  420. while (us) {
  421. u32 delay = 1000;
  422. if (delay > us)
  423. delay = us;
  424. udelay(delay);
  425. touch_nmi_watchdog();
  426. us -= delay;
  427. }
  428. }
  429. /*
  430. * Support ACPI 3.0 AML Timer operand
  431. * Returns 64-bit free-running, monotonically increasing timer
  432. * with 100ns granularity
  433. */
  434. u64 acpi_os_get_timer(void)
  435. {
  436. static u64 t;
  437. #ifdef CONFIG_HPET
  438. /* TBD: use HPET if available */
  439. #endif
  440. #ifdef CONFIG_X86_PM_TIMER
  441. /* TBD: default to PM timer if HPET was not available */
  442. #endif
  443. if (!t)
  444. printk(KERN_ERR PREFIX "acpi_os_get_timer() TBD\n");
  445. return ++t;
  446. }
  447. acpi_status acpi_os_read_port(acpi_io_address port, u32 * value, u32 width)
  448. {
  449. u32 dummy;
  450. if (!value)
  451. value = &dummy;
  452. *value = 0;
  453. if (width <= 8) {
  454. *(u8 *) value = inb(port);
  455. } else if (width <= 16) {
  456. *(u16 *) value = inw(port);
  457. } else if (width <= 32) {
  458. *(u32 *) value = inl(port);
  459. } else {
  460. BUG();
  461. }
  462. return AE_OK;
  463. }
  464. EXPORT_SYMBOL(acpi_os_read_port);
  465. acpi_status acpi_os_write_port(acpi_io_address port, u32 value, u32 width)
  466. {
  467. if (width <= 8) {
  468. outb(value, port);
  469. } else if (width <= 16) {
  470. outw(value, port);
  471. } else if (width <= 32) {
  472. outl(value, port);
  473. } else {
  474. BUG();
  475. }
  476. return AE_OK;
  477. }
  478. EXPORT_SYMBOL(acpi_os_write_port);
  479. acpi_status
  480. acpi_os_read_memory(acpi_physical_address phys_addr, u32 * value, u32 width)
  481. {
  482. u32 dummy;
  483. void __iomem *virt_addr;
  484. int size = width / 8, unmap = 0;
  485. unsigned long flags;
  486. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  487. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  488. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  489. if (!virt_addr) {
  490. virt_addr = ioremap(phys_addr, size);
  491. unmap = 1;
  492. }
  493. if (!value)
  494. value = &dummy;
  495. switch (width) {
  496. case 8:
  497. *(u8 *) value = readb(virt_addr);
  498. break;
  499. case 16:
  500. *(u16 *) value = readw(virt_addr);
  501. break;
  502. case 32:
  503. *(u32 *) value = readl(virt_addr);
  504. break;
  505. default:
  506. BUG();
  507. }
  508. if (unmap)
  509. iounmap(virt_addr);
  510. return AE_OK;
  511. }
  512. acpi_status
  513. acpi_os_write_memory(acpi_physical_address phys_addr, u32 value, u32 width)
  514. {
  515. void __iomem *virt_addr;
  516. int size = width / 8, unmap = 0;
  517. unsigned long flags;
  518. spin_lock_irqsave(&acpi_ioremap_lock, flags);
  519. virt_addr = acpi_map_vaddr_lookup(phys_addr, size);
  520. spin_unlock_irqrestore(&acpi_ioremap_lock, flags);
  521. if (!virt_addr) {
  522. virt_addr = ioremap(phys_addr, size);
  523. unmap = 1;
  524. }
  525. switch (width) {
  526. case 8:
  527. writeb(value, virt_addr);
  528. break;
  529. case 16:
  530. writew(value, virt_addr);
  531. break;
  532. case 32:
  533. writel(value, virt_addr);
  534. break;
  535. default:
  536. BUG();
  537. }
  538. if (unmap)
  539. iounmap(virt_addr);
  540. return AE_OK;
  541. }
  542. acpi_status
  543. acpi_os_read_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  544. u32 *value, u32 width)
  545. {
  546. int result, size;
  547. if (!value)
  548. return AE_BAD_PARAMETER;
  549. switch (width) {
  550. case 8:
  551. size = 1;
  552. break;
  553. case 16:
  554. size = 2;
  555. break;
  556. case 32:
  557. size = 4;
  558. break;
  559. default:
  560. return AE_ERROR;
  561. }
  562. result = raw_pci_read(pci_id->segment, pci_id->bus,
  563. PCI_DEVFN(pci_id->device, pci_id->function),
  564. reg, size, value);
  565. return (result ? AE_ERROR : AE_OK);
  566. }
  567. acpi_status
  568. acpi_os_write_pci_configuration(struct acpi_pci_id * pci_id, u32 reg,
  569. u64 value, u32 width)
  570. {
  571. int result, size;
  572. switch (width) {
  573. case 8:
  574. size = 1;
  575. break;
  576. case 16:
  577. size = 2;
  578. break;
  579. case 32:
  580. size = 4;
  581. break;
  582. default:
  583. return AE_ERROR;
  584. }
  585. result = raw_pci_write(pci_id->segment, pci_id->bus,
  586. PCI_DEVFN(pci_id->device, pci_id->function),
  587. reg, size, value);
  588. return (result ? AE_ERROR : AE_OK);
  589. }
  590. /* TODO: Change code to take advantage of driver model more */
  591. static void acpi_os_derive_pci_id_2(acpi_handle rhandle, /* upper bound */
  592. acpi_handle chandle, /* current node */
  593. struct acpi_pci_id **id,
  594. int *is_bridge, u8 * bus_number)
  595. {
  596. acpi_handle handle;
  597. struct acpi_pci_id *pci_id = *id;
  598. acpi_status status;
  599. unsigned long long temp;
  600. acpi_object_type type;
  601. acpi_get_parent(chandle, &handle);
  602. if (handle != rhandle) {
  603. acpi_os_derive_pci_id_2(rhandle, handle, &pci_id, is_bridge,
  604. bus_number);
  605. status = acpi_get_type(handle, &type);
  606. if ((ACPI_FAILURE(status)) || (type != ACPI_TYPE_DEVICE))
  607. return;
  608. status = acpi_evaluate_integer(handle, METHOD_NAME__ADR, NULL,
  609. &temp);
  610. if (ACPI_SUCCESS(status)) {
  611. u32 val;
  612. pci_id->device = ACPI_HIWORD(ACPI_LODWORD(temp));
  613. pci_id->function = ACPI_LOWORD(ACPI_LODWORD(temp));
  614. if (*is_bridge)
  615. pci_id->bus = *bus_number;
  616. /* any nicer way to get bus number of bridge ? */
  617. status =
  618. acpi_os_read_pci_configuration(pci_id, 0x0e, &val,
  619. 8);
  620. if (ACPI_SUCCESS(status)
  621. && ((val & 0x7f) == 1 || (val & 0x7f) == 2)) {
  622. status =
  623. acpi_os_read_pci_configuration(pci_id, 0x18,
  624. &val, 8);
  625. if (!ACPI_SUCCESS(status)) {
  626. /* Certainly broken... FIX ME */
  627. return;
  628. }
  629. *is_bridge = 1;
  630. pci_id->bus = val;
  631. status =
  632. acpi_os_read_pci_configuration(pci_id, 0x19,
  633. &val, 8);
  634. if (ACPI_SUCCESS(status)) {
  635. *bus_number = val;
  636. }
  637. } else
  638. *is_bridge = 0;
  639. }
  640. }
  641. }
  642. void acpi_os_derive_pci_id(acpi_handle rhandle, /* upper bound */
  643. acpi_handle chandle, /* current node */
  644. struct acpi_pci_id **id)
  645. {
  646. int is_bridge = 1;
  647. u8 bus_number = (*id)->bus;
  648. acpi_os_derive_pci_id_2(rhandle, chandle, id, &is_bridge, &bus_number);
  649. }
  650. static void acpi_os_execute_deferred(struct work_struct *work)
  651. {
  652. struct acpi_os_dpc *dpc = container_of(work, struct acpi_os_dpc, work);
  653. if (dpc->wait)
  654. acpi_os_wait_events_complete(NULL);
  655. dpc->function(dpc->context);
  656. kfree(dpc);
  657. }
  658. /*******************************************************************************
  659. *
  660. * FUNCTION: acpi_os_execute
  661. *
  662. * PARAMETERS: Type - Type of the callback
  663. * Function - Function to be executed
  664. * Context - Function parameters
  665. *
  666. * RETURN: Status
  667. *
  668. * DESCRIPTION: Depending on type, either queues function for deferred execution or
  669. * immediately executes function on a separate thread.
  670. *
  671. ******************************************************************************/
  672. static acpi_status __acpi_os_execute(acpi_execute_type type,
  673. acpi_osd_exec_callback function, void *context, int hp)
  674. {
  675. acpi_status status = AE_OK;
  676. struct acpi_os_dpc *dpc;
  677. struct workqueue_struct *queue;
  678. int ret;
  679. ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
  680. "Scheduling function [%p(%p)] for deferred execution.\n",
  681. function, context));
  682. /*
  683. * Allocate/initialize DPC structure. Note that this memory will be
  684. * freed by the callee. The kernel handles the work_struct list in a
  685. * way that allows us to also free its memory inside the callee.
  686. * Because we may want to schedule several tasks with different
  687. * parameters we can't use the approach some kernel code uses of
  688. * having a static work_struct.
  689. */
  690. dpc = kmalloc(sizeof(struct acpi_os_dpc), GFP_ATOMIC);
  691. if (!dpc)
  692. return AE_NO_MEMORY;
  693. dpc->function = function;
  694. dpc->context = context;
  695. /*
  696. * We can't run hotplug code in keventd_wq/kacpid_wq/kacpid_notify_wq
  697. * because the hotplug code may call driver .remove() functions,
  698. * which invoke flush_scheduled_work/acpi_os_wait_events_complete
  699. * to flush these workqueues.
  700. */
  701. queue = hp ? kacpi_hotplug_wq :
  702. (type == OSL_NOTIFY_HANDLER ? kacpi_notify_wq : kacpid_wq);
  703. dpc->wait = hp ? 1 : 0;
  704. if (queue == kacpi_hotplug_wq)
  705. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  706. else if (queue == kacpi_notify_wq)
  707. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  708. else
  709. INIT_WORK(&dpc->work, acpi_os_execute_deferred);
  710. /*
  711. * On some machines, a software-initiated SMI causes corruption unless
  712. * the SMI runs on CPU 0. An SMI can be initiated by any AML, but
  713. * typically it's done in GPE-related methods that are run via
  714. * workqueues, so we can avoid the known corruption cases by always
  715. * queueing on CPU 0.
  716. */
  717. ret = queue_work_on(0, queue, &dpc->work);
  718. if (!ret) {
  719. printk(KERN_ERR PREFIX
  720. "Call to queue_work() failed.\n");
  721. status = AE_ERROR;
  722. kfree(dpc);
  723. }
  724. return status;
  725. }
  726. acpi_status acpi_os_execute(acpi_execute_type type,
  727. acpi_osd_exec_callback function, void *context)
  728. {
  729. return __acpi_os_execute(type, function, context, 0);
  730. }
  731. EXPORT_SYMBOL(acpi_os_execute);
  732. acpi_status acpi_os_hotplug_execute(acpi_osd_exec_callback function,
  733. void *context)
  734. {
  735. return __acpi_os_execute(0, function, context, 1);
  736. }
  737. void acpi_os_wait_events_complete(void *context)
  738. {
  739. flush_workqueue(kacpid_wq);
  740. flush_workqueue(kacpi_notify_wq);
  741. }
  742. EXPORT_SYMBOL(acpi_os_wait_events_complete);
  743. /*
  744. * Allocate the memory for a spinlock and initialize it.
  745. */
  746. acpi_status acpi_os_create_lock(acpi_spinlock * handle)
  747. {
  748. spin_lock_init(*handle);
  749. return AE_OK;
  750. }
  751. /*
  752. * Deallocate the memory for a spinlock.
  753. */
  754. void acpi_os_delete_lock(acpi_spinlock handle)
  755. {
  756. return;
  757. }
  758. acpi_status
  759. acpi_os_create_semaphore(u32 max_units, u32 initial_units, acpi_handle * handle)
  760. {
  761. struct semaphore *sem = NULL;
  762. sem = acpi_os_allocate(sizeof(struct semaphore));
  763. if (!sem)
  764. return AE_NO_MEMORY;
  765. memset(sem, 0, sizeof(struct semaphore));
  766. sema_init(sem, initial_units);
  767. *handle = (acpi_handle *) sem;
  768. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Creating semaphore[%p|%d].\n",
  769. *handle, initial_units));
  770. return AE_OK;
  771. }
  772. /*
  773. * TODO: A better way to delete semaphores? Linux doesn't have a
  774. * 'delete_semaphore()' function -- may result in an invalid
  775. * pointer dereference for non-synchronized consumers. Should
  776. * we at least check for blocked threads and signal/cancel them?
  777. */
  778. acpi_status acpi_os_delete_semaphore(acpi_handle handle)
  779. {
  780. struct semaphore *sem = (struct semaphore *)handle;
  781. if (!sem)
  782. return AE_BAD_PARAMETER;
  783. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Deleting semaphore[%p].\n", handle));
  784. BUG_ON(!list_empty(&sem->wait_list));
  785. kfree(sem);
  786. sem = NULL;
  787. return AE_OK;
  788. }
  789. /*
  790. * TODO: Support for units > 1?
  791. */
  792. acpi_status acpi_os_wait_semaphore(acpi_handle handle, u32 units, u16 timeout)
  793. {
  794. acpi_status status = AE_OK;
  795. struct semaphore *sem = (struct semaphore *)handle;
  796. long jiffies;
  797. int ret = 0;
  798. if (!sem || (units < 1))
  799. return AE_BAD_PARAMETER;
  800. if (units > 1)
  801. return AE_SUPPORT;
  802. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Waiting for semaphore[%p|%d|%d]\n",
  803. handle, units, timeout));
  804. if (timeout == ACPI_WAIT_FOREVER)
  805. jiffies = MAX_SCHEDULE_TIMEOUT;
  806. else
  807. jiffies = msecs_to_jiffies(timeout);
  808. ret = down_timeout(sem, jiffies);
  809. if (ret)
  810. status = AE_TIME;
  811. if (ACPI_FAILURE(status)) {
  812. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  813. "Failed to acquire semaphore[%p|%d|%d], %s",
  814. handle, units, timeout,
  815. acpi_format_exception(status)));
  816. } else {
  817. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX,
  818. "Acquired semaphore[%p|%d|%d]", handle,
  819. units, timeout));
  820. }
  821. return status;
  822. }
  823. /*
  824. * TODO: Support for units > 1?
  825. */
  826. acpi_status acpi_os_signal_semaphore(acpi_handle handle, u32 units)
  827. {
  828. struct semaphore *sem = (struct semaphore *)handle;
  829. if (!sem || (units < 1))
  830. return AE_BAD_PARAMETER;
  831. if (units > 1)
  832. return AE_SUPPORT;
  833. ACPI_DEBUG_PRINT((ACPI_DB_MUTEX, "Signaling semaphore[%p|%d]\n", handle,
  834. units));
  835. up(sem);
  836. return AE_OK;
  837. }
  838. #ifdef ACPI_FUTURE_USAGE
  839. u32 acpi_os_get_line(char *buffer)
  840. {
  841. #ifdef ENABLE_DEBUGGER
  842. if (acpi_in_debugger) {
  843. u32 chars;
  844. kdb_read(buffer, sizeof(line_buf));
  845. /* remove the CR kdb includes */
  846. chars = strlen(buffer) - 1;
  847. buffer[chars] = '\0';
  848. }
  849. #endif
  850. return 0;
  851. }
  852. #endif /* ACPI_FUTURE_USAGE */
  853. acpi_status acpi_os_signal(u32 function, void *info)
  854. {
  855. switch (function) {
  856. case ACPI_SIGNAL_FATAL:
  857. printk(KERN_ERR PREFIX "Fatal opcode executed\n");
  858. break;
  859. case ACPI_SIGNAL_BREAKPOINT:
  860. /*
  861. * AML Breakpoint
  862. * ACPI spec. says to treat it as a NOP unless
  863. * you are debugging. So if/when we integrate
  864. * AML debugger into the kernel debugger its
  865. * hook will go here. But until then it is
  866. * not useful to print anything on breakpoints.
  867. */
  868. break;
  869. default:
  870. break;
  871. }
  872. return AE_OK;
  873. }
  874. static int __init acpi_os_name_setup(char *str)
  875. {
  876. char *p = acpi_os_name;
  877. int count = ACPI_MAX_OVERRIDE_LEN - 1;
  878. if (!str || !*str)
  879. return 0;
  880. for (; count-- && str && *str; str++) {
  881. if (isalnum(*str) || *str == ' ' || *str == ':')
  882. *p++ = *str;
  883. else if (*str == '\'' || *str == '"')
  884. continue;
  885. else
  886. break;
  887. }
  888. *p = 0;
  889. return 1;
  890. }
  891. __setup("acpi_os_name=", acpi_os_name_setup);
  892. static void __init set_osi_linux(unsigned int enable)
  893. {
  894. if (osi_linux.enable != enable) {
  895. osi_linux.enable = enable;
  896. printk(KERN_NOTICE PREFIX "%sed _OSI(Linux)\n",
  897. enable ? "Add": "Delet");
  898. }
  899. return;
  900. }
  901. static void __init acpi_cmdline_osi_linux(unsigned int enable)
  902. {
  903. osi_linux.cmdline = 1; /* cmdline set the default */
  904. set_osi_linux(enable);
  905. return;
  906. }
  907. void __init acpi_dmi_osi_linux(int enable, const struct dmi_system_id *d)
  908. {
  909. osi_linux.dmi = 1; /* DMI knows that this box asks OSI(Linux) */
  910. printk(KERN_NOTICE PREFIX "DMI detected: %s\n", d->ident);
  911. if (enable == -1)
  912. return;
  913. osi_linux.known = 1; /* DMI knows which OSI(Linux) default needed */
  914. set_osi_linux(enable);
  915. return;
  916. }
  917. /*
  918. * Modify the list of "OS Interfaces" reported to BIOS via _OSI
  919. *
  920. * empty string disables _OSI
  921. * string starting with '!' disables that string
  922. * otherwise string is added to list, augmenting built-in strings
  923. */
  924. int __init acpi_osi_setup(char *str)
  925. {
  926. if (str == NULL || *str == '\0') {
  927. printk(KERN_INFO PREFIX "_OSI method disabled\n");
  928. acpi_gbl_create_osi_method = FALSE;
  929. } else if (!strcmp("!Linux", str)) {
  930. acpi_cmdline_osi_linux(0); /* !enable */
  931. } else if (*str == '!') {
  932. if (acpi_osi_invalidate(++str) == AE_OK)
  933. printk(KERN_INFO PREFIX "Deleted _OSI(%s)\n", str);
  934. } else if (!strcmp("Linux", str)) {
  935. acpi_cmdline_osi_linux(1); /* enable */
  936. } else if (*osi_additional_string == '\0') {
  937. strncpy(osi_additional_string, str, OSI_STRING_LENGTH_MAX);
  938. printk(KERN_INFO PREFIX "Added _OSI(%s)\n", str);
  939. }
  940. return 1;
  941. }
  942. __setup("acpi_osi=", acpi_osi_setup);
  943. /* enable serialization to combat AE_ALREADY_EXISTS errors */
  944. static int __init acpi_serialize_setup(char *str)
  945. {
  946. printk(KERN_INFO PREFIX "serialize enabled\n");
  947. acpi_gbl_all_methods_serialized = TRUE;
  948. return 1;
  949. }
  950. __setup("acpi_serialize", acpi_serialize_setup);
  951. /* Check of resource interference between native drivers and ACPI
  952. * OperationRegions (SystemIO and System Memory only).
  953. * IO ports and memory declared in ACPI might be used by the ACPI subsystem
  954. * in arbitrary AML code and can interfere with legacy drivers.
  955. * acpi_enforce_resources= can be set to:
  956. *
  957. * - strict (default) (2)
  958. * -> further driver trying to access the resources will not load
  959. * - lax (1)
  960. * -> further driver trying to access the resources will load, but you
  961. * get a system message that something might go wrong...
  962. *
  963. * - no (0)
  964. * -> ACPI Operation Region resources will not be registered
  965. *
  966. */
  967. #define ENFORCE_RESOURCES_STRICT 2
  968. #define ENFORCE_RESOURCES_LAX 1
  969. #define ENFORCE_RESOURCES_NO 0
  970. static unsigned int acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  971. static int __init acpi_enforce_resources_setup(char *str)
  972. {
  973. if (str == NULL || *str == '\0')
  974. return 0;
  975. if (!strcmp("strict", str))
  976. acpi_enforce_resources = ENFORCE_RESOURCES_STRICT;
  977. else if (!strcmp("lax", str))
  978. acpi_enforce_resources = ENFORCE_RESOURCES_LAX;
  979. else if (!strcmp("no", str))
  980. acpi_enforce_resources = ENFORCE_RESOURCES_NO;
  981. return 1;
  982. }
  983. __setup("acpi_enforce_resources=", acpi_enforce_resources_setup);
  984. /* Check for resource conflicts between ACPI OperationRegions and native
  985. * drivers */
  986. int acpi_check_resource_conflict(const struct resource *res)
  987. {
  988. struct acpi_res_list *res_list_elem;
  989. int ioport;
  990. int clash = 0;
  991. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  992. return 0;
  993. if (!(res->flags & IORESOURCE_IO) && !(res->flags & IORESOURCE_MEM))
  994. return 0;
  995. ioport = res->flags & IORESOURCE_IO;
  996. spin_lock(&acpi_res_lock);
  997. list_for_each_entry(res_list_elem, &resource_list_head,
  998. resource_list) {
  999. if (ioport && (res_list_elem->resource_type
  1000. != ACPI_ADR_SPACE_SYSTEM_IO))
  1001. continue;
  1002. if (!ioport && (res_list_elem->resource_type
  1003. != ACPI_ADR_SPACE_SYSTEM_MEMORY))
  1004. continue;
  1005. if (res->end < res_list_elem->start
  1006. || res_list_elem->end < res->start)
  1007. continue;
  1008. clash = 1;
  1009. break;
  1010. }
  1011. spin_unlock(&acpi_res_lock);
  1012. if (clash) {
  1013. if (acpi_enforce_resources != ENFORCE_RESOURCES_NO) {
  1014. printk(KERN_WARNING "ACPI: resource %s %pR"
  1015. " conflicts with ACPI region %s %pR\n",
  1016. res->name, res, res_list_elem->name,
  1017. res_list_elem);
  1018. if (acpi_enforce_resources == ENFORCE_RESOURCES_LAX)
  1019. printk(KERN_NOTICE "ACPI: This conflict may"
  1020. " cause random problems and system"
  1021. " instability\n");
  1022. printk(KERN_INFO "ACPI: If an ACPI driver is available"
  1023. " for this device, you should use it instead of"
  1024. " the native driver\n");
  1025. }
  1026. if (acpi_enforce_resources == ENFORCE_RESOURCES_STRICT)
  1027. return -EBUSY;
  1028. }
  1029. return 0;
  1030. }
  1031. EXPORT_SYMBOL(acpi_check_resource_conflict);
  1032. int acpi_check_region(resource_size_t start, resource_size_t n,
  1033. const char *name)
  1034. {
  1035. struct resource res = {
  1036. .start = start,
  1037. .end = start + n - 1,
  1038. .name = name,
  1039. .flags = IORESOURCE_IO,
  1040. };
  1041. return acpi_check_resource_conflict(&res);
  1042. }
  1043. EXPORT_SYMBOL(acpi_check_region);
  1044. int acpi_check_mem_region(resource_size_t start, resource_size_t n,
  1045. const char *name)
  1046. {
  1047. struct resource res = {
  1048. .start = start,
  1049. .end = start + n - 1,
  1050. .name = name,
  1051. .flags = IORESOURCE_MEM,
  1052. };
  1053. return acpi_check_resource_conflict(&res);
  1054. }
  1055. EXPORT_SYMBOL(acpi_check_mem_region);
  1056. /*
  1057. * Let drivers know whether the resource checks are effective
  1058. */
  1059. int acpi_resources_are_enforced(void)
  1060. {
  1061. return acpi_enforce_resources == ENFORCE_RESOURCES_STRICT;
  1062. }
  1063. EXPORT_SYMBOL(acpi_resources_are_enforced);
  1064. /*
  1065. * Acquire a spinlock.
  1066. *
  1067. * handle is a pointer to the spinlock_t.
  1068. */
  1069. acpi_cpu_flags acpi_os_acquire_lock(acpi_spinlock lockp)
  1070. {
  1071. acpi_cpu_flags flags;
  1072. spin_lock_irqsave(lockp, flags);
  1073. return flags;
  1074. }
  1075. /*
  1076. * Release a spinlock. See above.
  1077. */
  1078. void acpi_os_release_lock(acpi_spinlock lockp, acpi_cpu_flags flags)
  1079. {
  1080. spin_unlock_irqrestore(lockp, flags);
  1081. }
  1082. #ifndef ACPI_USE_LOCAL_CACHE
  1083. /*******************************************************************************
  1084. *
  1085. * FUNCTION: acpi_os_create_cache
  1086. *
  1087. * PARAMETERS: name - Ascii name for the cache
  1088. * size - Size of each cached object
  1089. * depth - Maximum depth of the cache (in objects) <ignored>
  1090. * cache - Where the new cache object is returned
  1091. *
  1092. * RETURN: status
  1093. *
  1094. * DESCRIPTION: Create a cache object
  1095. *
  1096. ******************************************************************************/
  1097. acpi_status
  1098. acpi_os_create_cache(char *name, u16 size, u16 depth, acpi_cache_t ** cache)
  1099. {
  1100. *cache = kmem_cache_create(name, size, 0, 0, NULL);
  1101. if (*cache == NULL)
  1102. return AE_ERROR;
  1103. else
  1104. return AE_OK;
  1105. }
  1106. /*******************************************************************************
  1107. *
  1108. * FUNCTION: acpi_os_purge_cache
  1109. *
  1110. * PARAMETERS: Cache - Handle to cache object
  1111. *
  1112. * RETURN: Status
  1113. *
  1114. * DESCRIPTION: Free all objects within the requested cache.
  1115. *
  1116. ******************************************************************************/
  1117. acpi_status acpi_os_purge_cache(acpi_cache_t * cache)
  1118. {
  1119. kmem_cache_shrink(cache);
  1120. return (AE_OK);
  1121. }
  1122. /*******************************************************************************
  1123. *
  1124. * FUNCTION: acpi_os_delete_cache
  1125. *
  1126. * PARAMETERS: Cache - Handle to cache object
  1127. *
  1128. * RETURN: Status
  1129. *
  1130. * DESCRIPTION: Free all objects within the requested cache and delete the
  1131. * cache object.
  1132. *
  1133. ******************************************************************************/
  1134. acpi_status acpi_os_delete_cache(acpi_cache_t * cache)
  1135. {
  1136. kmem_cache_destroy(cache);
  1137. return (AE_OK);
  1138. }
  1139. /*******************************************************************************
  1140. *
  1141. * FUNCTION: acpi_os_release_object
  1142. *
  1143. * PARAMETERS: Cache - Handle to cache object
  1144. * Object - The object to be released
  1145. *
  1146. * RETURN: None
  1147. *
  1148. * DESCRIPTION: Release an object to the specified cache. If cache is full,
  1149. * the object is deleted.
  1150. *
  1151. ******************************************************************************/
  1152. acpi_status acpi_os_release_object(acpi_cache_t * cache, void *object)
  1153. {
  1154. kmem_cache_free(cache, object);
  1155. return (AE_OK);
  1156. }
  1157. /******************************************************************************
  1158. *
  1159. * FUNCTION: acpi_os_validate_interface
  1160. *
  1161. * PARAMETERS: interface - Requested interface to be validated
  1162. *
  1163. * RETURN: AE_OK if interface is supported, AE_SUPPORT otherwise
  1164. *
  1165. * DESCRIPTION: Match an interface string to the interfaces supported by the
  1166. * host. Strings originate from an AML call to the _OSI method.
  1167. *
  1168. *****************************************************************************/
  1169. acpi_status
  1170. acpi_os_validate_interface (char *interface)
  1171. {
  1172. if (!strncmp(osi_additional_string, interface, OSI_STRING_LENGTH_MAX))
  1173. return AE_OK;
  1174. if (!strcmp("Linux", interface)) {
  1175. printk(KERN_NOTICE PREFIX
  1176. "BIOS _OSI(Linux) query %s%s\n",
  1177. osi_linux.enable ? "honored" : "ignored",
  1178. osi_linux.cmdline ? " via cmdline" :
  1179. osi_linux.dmi ? " via DMI" : "");
  1180. if (osi_linux.enable)
  1181. return AE_OK;
  1182. }
  1183. return AE_SUPPORT;
  1184. }
  1185. static inline int acpi_res_list_add(struct acpi_res_list *res)
  1186. {
  1187. struct acpi_res_list *res_list_elem;
  1188. list_for_each_entry(res_list_elem, &resource_list_head,
  1189. resource_list) {
  1190. if (res->resource_type == res_list_elem->resource_type &&
  1191. res->start == res_list_elem->start &&
  1192. res->end == res_list_elem->end) {
  1193. /*
  1194. * The Region(addr,len) already exist in the list,
  1195. * just increase the count
  1196. */
  1197. res_list_elem->count++;
  1198. return 0;
  1199. }
  1200. }
  1201. res->count = 1;
  1202. list_add(&res->resource_list, &resource_list_head);
  1203. return 1;
  1204. }
  1205. static inline void acpi_res_list_del(struct acpi_res_list *res)
  1206. {
  1207. struct acpi_res_list *res_list_elem;
  1208. list_for_each_entry(res_list_elem, &resource_list_head,
  1209. resource_list) {
  1210. if (res->resource_type == res_list_elem->resource_type &&
  1211. res->start == res_list_elem->start &&
  1212. res->end == res_list_elem->end) {
  1213. /*
  1214. * If the res count is decreased to 0,
  1215. * remove and free it
  1216. */
  1217. if (--res_list_elem->count == 0) {
  1218. list_del(&res_list_elem->resource_list);
  1219. kfree(res_list_elem);
  1220. }
  1221. return;
  1222. }
  1223. }
  1224. }
  1225. acpi_status
  1226. acpi_os_invalidate_address(
  1227. u8 space_id,
  1228. acpi_physical_address address,
  1229. acpi_size length)
  1230. {
  1231. struct acpi_res_list res;
  1232. switch (space_id) {
  1233. case ACPI_ADR_SPACE_SYSTEM_IO:
  1234. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  1235. /* Only interference checks against SystemIO and SystemMemory
  1236. are needed */
  1237. res.start = address;
  1238. res.end = address + length - 1;
  1239. res.resource_type = space_id;
  1240. spin_lock(&acpi_res_lock);
  1241. acpi_res_list_del(&res);
  1242. spin_unlock(&acpi_res_lock);
  1243. break;
  1244. case ACPI_ADR_SPACE_PCI_CONFIG:
  1245. case ACPI_ADR_SPACE_EC:
  1246. case ACPI_ADR_SPACE_SMBUS:
  1247. case ACPI_ADR_SPACE_CMOS:
  1248. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  1249. case ACPI_ADR_SPACE_DATA_TABLE:
  1250. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  1251. break;
  1252. }
  1253. return AE_OK;
  1254. }
  1255. /******************************************************************************
  1256. *
  1257. * FUNCTION: acpi_os_validate_address
  1258. *
  1259. * PARAMETERS: space_id - ACPI space ID
  1260. * address - Physical address
  1261. * length - Address length
  1262. *
  1263. * RETURN: AE_OK if address/length is valid for the space_id. Otherwise,
  1264. * should return AE_AML_ILLEGAL_ADDRESS.
  1265. *
  1266. * DESCRIPTION: Validate a system address via the host OS. Used to validate
  1267. * the addresses accessed by AML operation regions.
  1268. *
  1269. *****************************************************************************/
  1270. acpi_status
  1271. acpi_os_validate_address (
  1272. u8 space_id,
  1273. acpi_physical_address address,
  1274. acpi_size length,
  1275. char *name)
  1276. {
  1277. struct acpi_res_list *res;
  1278. int added;
  1279. if (acpi_enforce_resources == ENFORCE_RESOURCES_NO)
  1280. return AE_OK;
  1281. switch (space_id) {
  1282. case ACPI_ADR_SPACE_SYSTEM_IO:
  1283. case ACPI_ADR_SPACE_SYSTEM_MEMORY:
  1284. /* Only interference checks against SystemIO and SystemMemory
  1285. are needed */
  1286. res = kzalloc(sizeof(struct acpi_res_list), GFP_KERNEL);
  1287. if (!res)
  1288. return AE_OK;
  1289. /* ACPI names are fixed to 4 bytes, still better use strlcpy */
  1290. strlcpy(res->name, name, 5);
  1291. res->start = address;
  1292. res->end = address + length - 1;
  1293. res->resource_type = space_id;
  1294. spin_lock(&acpi_res_lock);
  1295. added = acpi_res_list_add(res);
  1296. spin_unlock(&acpi_res_lock);
  1297. pr_debug("%s %s resource: start: 0x%llx, end: 0x%llx, "
  1298. "name: %s\n", added ? "Added" : "Already exist",
  1299. (space_id == ACPI_ADR_SPACE_SYSTEM_IO)
  1300. ? "SystemIO" : "System Memory",
  1301. (unsigned long long)res->start,
  1302. (unsigned long long)res->end,
  1303. res->name);
  1304. if (!added)
  1305. kfree(res);
  1306. break;
  1307. case ACPI_ADR_SPACE_PCI_CONFIG:
  1308. case ACPI_ADR_SPACE_EC:
  1309. case ACPI_ADR_SPACE_SMBUS:
  1310. case ACPI_ADR_SPACE_CMOS:
  1311. case ACPI_ADR_SPACE_PCI_BAR_TARGET:
  1312. case ACPI_ADR_SPACE_DATA_TABLE:
  1313. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  1314. break;
  1315. }
  1316. return AE_OK;
  1317. }
  1318. #endif
  1319. acpi_status __init acpi_os_initialize(void)
  1320. {
  1321. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1322. acpi_os_map_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1323. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1324. acpi_os_map_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1325. return AE_OK;
  1326. }
  1327. acpi_status acpi_os_initialize1(void)
  1328. {
  1329. kacpid_wq = create_workqueue("kacpid");
  1330. kacpi_notify_wq = create_workqueue("kacpi_notify");
  1331. kacpi_hotplug_wq = create_workqueue("kacpi_hotplug");
  1332. BUG_ON(!kacpid_wq);
  1333. BUG_ON(!kacpi_notify_wq);
  1334. BUG_ON(!kacpi_hotplug_wq);
  1335. return AE_OK;
  1336. }
  1337. acpi_status acpi_os_terminate(void)
  1338. {
  1339. if (acpi_irq_handler) {
  1340. acpi_os_remove_interrupt_handler(acpi_irq_irq,
  1341. acpi_irq_handler);
  1342. }
  1343. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe1_block);
  1344. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xgpe0_block);
  1345. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1b_event_block);
  1346. acpi_os_unmap_generic_address(&acpi_gbl_FADT.xpm1a_event_block);
  1347. destroy_workqueue(kacpid_wq);
  1348. destroy_workqueue(kacpi_notify_wq);
  1349. destroy_workqueue(kacpi_hotplug_wq);
  1350. return AE_OK;
  1351. }