osl.c 38 KB

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