osl.c 40 KB

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