cpqphp_nvram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * Compaq Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. *
  8. * All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or (at
  13. * your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  18. * NON INFRINGEMENT. See the GNU General Public License for more
  19. * details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. *
  25. * Send feedback to <greg@kroah.com>
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/proc_fs.h>
  32. #include <linux/slab.h>
  33. #include <linux/workqueue.h>
  34. #include <linux/pci.h>
  35. #include <linux/pci_hotplug.h>
  36. #include <asm/uaccess.h>
  37. #include "cpqphp.h"
  38. #include "cpqphp_nvram.h"
  39. #define ROM_INT15_PHY_ADDR 0x0FF859
  40. #define READ_EV 0xD8A4
  41. #define WRITE_EV 0xD8A5
  42. struct register_foo {
  43. union {
  44. unsigned long lword; /* eax */
  45. unsigned short word; /* ax */
  46. struct {
  47. unsigned char low; /* al */
  48. unsigned char high; /* ah */
  49. } byte;
  50. } data;
  51. unsigned char opcode; /* see below */
  52. unsigned long length; /* if the reg. is a pointer, how much data */
  53. } __attribute__ ((packed));
  54. struct all_reg {
  55. struct register_foo eax_reg;
  56. struct register_foo ebx_reg;
  57. struct register_foo ecx_reg;
  58. struct register_foo edx_reg;
  59. struct register_foo edi_reg;
  60. struct register_foo esi_reg;
  61. struct register_foo eflags_reg;
  62. } __attribute__ ((packed));
  63. struct ev_hrt_header {
  64. u8 Version;
  65. u8 num_of_ctrl;
  66. u8 next;
  67. };
  68. struct ev_hrt_ctrl {
  69. u8 bus;
  70. u8 device;
  71. u8 function;
  72. u8 mem_avail;
  73. u8 p_mem_avail;
  74. u8 io_avail;
  75. u8 bus_avail;
  76. u8 next;
  77. };
  78. static u8 evbuffer_init;
  79. static u8 evbuffer_length;
  80. static u8 evbuffer[1024];
  81. static void __iomem *compaq_int15_entry_point;
  82. /* lock for ordering int15_bios_call() */
  83. static spinlock_t int15_lock;
  84. /* This is a series of function that deals with
  85. * setting & getting the hotplug resource table in some environment variable.
  86. */
  87. /*
  88. * We really shouldn't be doing this unless there is a _very_ good reason to!!!
  89. * greg k-h
  90. */
  91. static u32 add_byte(u32 **p_buffer, u8 value, u32 *used, u32 *avail)
  92. {
  93. u8 **tByte;
  94. if ((*used + 1) > *avail)
  95. return(1);
  96. *((u8*)*p_buffer) = value;
  97. tByte = (u8**)p_buffer;
  98. (*tByte)++;
  99. *used+=1;
  100. return(0);
  101. }
  102. static u32 add_dword(u32 **p_buffer, u32 value, u32 *used, u32 *avail)
  103. {
  104. if ((*used + 4) > *avail)
  105. return(1);
  106. **p_buffer = value;
  107. (*p_buffer)++;
  108. *used+=4;
  109. return(0);
  110. }
  111. /*
  112. * check_for_compaq_ROM
  113. *
  114. * this routine verifies that the ROM OEM string is 'COMPAQ'
  115. *
  116. * returns 0 for non-Compaq ROM, 1 for Compaq ROM
  117. */
  118. static int check_for_compaq_ROM (void __iomem *rom_start)
  119. {
  120. u8 temp1, temp2, temp3, temp4, temp5, temp6;
  121. int result = 0;
  122. temp1 = readb(rom_start + 0xffea + 0);
  123. temp2 = readb(rom_start + 0xffea + 1);
  124. temp3 = readb(rom_start + 0xffea + 2);
  125. temp4 = readb(rom_start + 0xffea + 3);
  126. temp5 = readb(rom_start + 0xffea + 4);
  127. temp6 = readb(rom_start + 0xffea + 5);
  128. if ((temp1 == 'C') &&
  129. (temp2 == 'O') &&
  130. (temp3 == 'M') &&
  131. (temp4 == 'P') &&
  132. (temp5 == 'A') &&
  133. (temp6 == 'Q')) {
  134. result = 1;
  135. }
  136. dbg ("%s - returned %d\n", __func__, result);
  137. return result;
  138. }
  139. static u32 access_EV (u16 operation, u8 *ev_name, u8 *buffer, u32 *buf_size)
  140. {
  141. unsigned long flags;
  142. int op = operation;
  143. int ret_val;
  144. if (!compaq_int15_entry_point)
  145. return -ENODEV;
  146. spin_lock_irqsave(&int15_lock, flags);
  147. __asm__ (
  148. "xorl %%ebx,%%ebx\n" \
  149. "xorl %%edx,%%edx\n" \
  150. "pushf\n" \
  151. "push %%cs\n" \
  152. "cli\n" \
  153. "call *%6\n"
  154. : "=c" (*buf_size), "=a" (ret_val)
  155. : "a" (op), "c" (*buf_size), "S" (ev_name),
  156. "D" (buffer), "m" (compaq_int15_entry_point)
  157. : "%ebx", "%edx");
  158. spin_unlock_irqrestore(&int15_lock, flags);
  159. return((ret_val & 0xFF00) >> 8);
  160. }
  161. /*
  162. * load_HRT
  163. *
  164. * Read the hot plug Resource Table from NVRAM
  165. */
  166. static int load_HRT (void __iomem *rom_start)
  167. {
  168. u32 available;
  169. u32 temp_dword;
  170. u8 temp_byte = 0xFF;
  171. u32 rc;
  172. if (!check_for_compaq_ROM(rom_start))
  173. return -ENODEV;
  174. available = 1024;
  175. /* Now load the EV */
  176. temp_dword = available;
  177. rc = access_EV(READ_EV, "CQTHPS", evbuffer, &temp_dword);
  178. evbuffer_length = temp_dword;
  179. /* We're maintaining the resource lists so write FF to invalidate old
  180. * info
  181. */
  182. temp_dword = 1;
  183. rc = access_EV(WRITE_EV, "CQTHPS", &temp_byte, &temp_dword);
  184. return rc;
  185. }
  186. /*
  187. * store_HRT
  188. *
  189. * Save the hot plug Resource Table in NVRAM
  190. */
  191. static u32 store_HRT (void __iomem *rom_start)
  192. {
  193. u32 *buffer;
  194. u32 *pFill;
  195. u32 usedbytes;
  196. u32 available;
  197. u32 temp_dword;
  198. u32 rc;
  199. u8 loop;
  200. u8 numCtrl = 0;
  201. struct controller *ctrl;
  202. struct pci_resource *resNode;
  203. struct ev_hrt_header *p_EV_header;
  204. struct ev_hrt_ctrl *p_ev_ctrl;
  205. available = 1024;
  206. if (!check_for_compaq_ROM(rom_start))
  207. return(1);
  208. buffer = (u32*) evbuffer;
  209. if (!buffer)
  210. return(1);
  211. pFill = buffer;
  212. usedbytes = 0;
  213. p_EV_header = (struct ev_hrt_header *) pFill;
  214. ctrl = cpqhp_ctrl_list;
  215. /* The revision of this structure */
  216. rc = add_byte(&pFill, 1 + ctrl->push_flag, &usedbytes, &available);
  217. if (rc)
  218. return(rc);
  219. /* The number of controllers */
  220. rc = add_byte(&pFill, 1, &usedbytes, &available);
  221. if (rc)
  222. return(rc);
  223. while (ctrl) {
  224. p_ev_ctrl = (struct ev_hrt_ctrl *) pFill;
  225. numCtrl++;
  226. /* The bus number */
  227. rc = add_byte(&pFill, ctrl->bus, &usedbytes, &available);
  228. if (rc)
  229. return(rc);
  230. /* The device Number */
  231. rc = add_byte(&pFill, PCI_SLOT(ctrl->pci_dev->devfn), &usedbytes, &available);
  232. if (rc)
  233. return(rc);
  234. /* The function Number */
  235. rc = add_byte(&pFill, PCI_FUNC(ctrl->pci_dev->devfn), &usedbytes, &available);
  236. if (rc)
  237. return(rc);
  238. /* Skip the number of available entries */
  239. rc = add_dword(&pFill, 0, &usedbytes, &available);
  240. if (rc)
  241. return(rc);
  242. /* Figure out memory Available */
  243. resNode = ctrl->mem_head;
  244. loop = 0;
  245. while (resNode) {
  246. loop ++;
  247. /* base */
  248. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  249. if (rc)
  250. return(rc);
  251. /* length */
  252. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  253. if (rc)
  254. return(rc);
  255. resNode = resNode->next;
  256. }
  257. /* Fill in the number of entries */
  258. p_ev_ctrl->mem_avail = loop;
  259. /* Figure out prefetchable memory Available */
  260. resNode = ctrl->p_mem_head;
  261. loop = 0;
  262. while (resNode) {
  263. loop ++;
  264. /* base */
  265. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  266. if (rc)
  267. return(rc);
  268. /* length */
  269. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  270. if (rc)
  271. return(rc);
  272. resNode = resNode->next;
  273. }
  274. /* Fill in the number of entries */
  275. p_ev_ctrl->p_mem_avail = loop;
  276. /* Figure out IO Available */
  277. resNode = ctrl->io_head;
  278. loop = 0;
  279. while (resNode) {
  280. loop ++;
  281. /* base */
  282. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  283. if (rc)
  284. return(rc);
  285. /* length */
  286. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  287. if (rc)
  288. return(rc);
  289. resNode = resNode->next;
  290. }
  291. /* Fill in the number of entries */
  292. p_ev_ctrl->io_avail = loop;
  293. /* Figure out bus Available */
  294. resNode = ctrl->bus_head;
  295. loop = 0;
  296. while (resNode) {
  297. loop ++;
  298. /* base */
  299. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  300. if (rc)
  301. return(rc);
  302. /* length */
  303. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  304. if (rc)
  305. return(rc);
  306. resNode = resNode->next;
  307. }
  308. /* Fill in the number of entries */
  309. p_ev_ctrl->bus_avail = loop;
  310. ctrl = ctrl->next;
  311. }
  312. p_EV_header->num_of_ctrl = numCtrl;
  313. /* Now store the EV */
  314. temp_dword = usedbytes;
  315. rc = access_EV(WRITE_EV, "CQTHPS", (u8*) buffer, &temp_dword);
  316. dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes, temp_dword);
  317. evbuffer_length = temp_dword;
  318. if (rc) {
  319. err(msg_unable_to_save);
  320. return(1);
  321. }
  322. return(0);
  323. }
  324. void compaq_nvram_init (void __iomem *rom_start)
  325. {
  326. if (rom_start)
  327. compaq_int15_entry_point = (rom_start + ROM_INT15_PHY_ADDR - ROM_PHY_ADDR);
  328. dbg("int15 entry = %p\n", compaq_int15_entry_point);
  329. /* initialize our int15 lock */
  330. spin_lock_init(&int15_lock);
  331. }
  332. int compaq_nvram_load (void __iomem *rom_start, struct controller *ctrl)
  333. {
  334. u8 bus, device, function;
  335. u8 nummem, numpmem, numio, numbus;
  336. u32 rc;
  337. u8 *p_byte;
  338. struct pci_resource *mem_node;
  339. struct pci_resource *p_mem_node;
  340. struct pci_resource *io_node;
  341. struct pci_resource *bus_node;
  342. struct ev_hrt_ctrl *p_ev_ctrl;
  343. struct ev_hrt_header *p_EV_header;
  344. if (!evbuffer_init) {
  345. /* Read the resource list information in from NVRAM */
  346. if (load_HRT(rom_start))
  347. memset (evbuffer, 0, 1024);
  348. evbuffer_init = 1;
  349. }
  350. /* If we saved information in NVRAM, use it now */
  351. p_EV_header = (struct ev_hrt_header *) evbuffer;
  352. /* The following code is for systems where version 1.0 of this
  353. * driver has been loaded, but doesn't support the hardware.
  354. * In that case, the driver would incorrectly store something
  355. * in NVRAM.
  356. */
  357. if ((p_EV_header->Version == 2) ||
  358. ((p_EV_header->Version == 1) && !ctrl->push_flag)) {
  359. p_byte = &(p_EV_header->next);
  360. p_ev_ctrl = (struct ev_hrt_ctrl *) &(p_EV_header->next);
  361. p_byte += 3;
  362. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  363. return 2;
  364. bus = p_ev_ctrl->bus;
  365. device = p_ev_ctrl->device;
  366. function = p_ev_ctrl->function;
  367. while ((bus != ctrl->bus) ||
  368. (device != PCI_SLOT(ctrl->pci_dev->devfn)) ||
  369. (function != PCI_FUNC(ctrl->pci_dev->devfn))) {
  370. nummem = p_ev_ctrl->mem_avail;
  371. numpmem = p_ev_ctrl->p_mem_avail;
  372. numio = p_ev_ctrl->io_avail;
  373. numbus = p_ev_ctrl->bus_avail;
  374. p_byte += 4;
  375. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  376. return 2;
  377. /* Skip forward to the next entry */
  378. p_byte += (nummem + numpmem + numio + numbus) * 8;
  379. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  380. return 2;
  381. p_ev_ctrl = (struct ev_hrt_ctrl *) p_byte;
  382. p_byte += 3;
  383. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  384. return 2;
  385. bus = p_ev_ctrl->bus;
  386. device = p_ev_ctrl->device;
  387. function = p_ev_ctrl->function;
  388. }
  389. nummem = p_ev_ctrl->mem_avail;
  390. numpmem = p_ev_ctrl->p_mem_avail;
  391. numio = p_ev_ctrl->io_avail;
  392. numbus = p_ev_ctrl->bus_avail;
  393. p_byte += 4;
  394. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  395. return 2;
  396. while (nummem--) {
  397. mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  398. if (!mem_node)
  399. break;
  400. mem_node->base = *(u32*)p_byte;
  401. dbg("mem base = %8.8x\n",mem_node->base);
  402. p_byte += 4;
  403. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  404. kfree(mem_node);
  405. return 2;
  406. }
  407. mem_node->length = *(u32*)p_byte;
  408. dbg("mem length = %8.8x\n",mem_node->length);
  409. p_byte += 4;
  410. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  411. kfree(mem_node);
  412. return 2;
  413. }
  414. mem_node->next = ctrl->mem_head;
  415. ctrl->mem_head = mem_node;
  416. }
  417. while (numpmem--) {
  418. p_mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  419. if (!p_mem_node)
  420. break;
  421. p_mem_node->base = *(u32*)p_byte;
  422. dbg("pre-mem base = %8.8x\n",p_mem_node->base);
  423. p_byte += 4;
  424. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  425. kfree(p_mem_node);
  426. return 2;
  427. }
  428. p_mem_node->length = *(u32*)p_byte;
  429. dbg("pre-mem length = %8.8x\n",p_mem_node->length);
  430. p_byte += 4;
  431. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  432. kfree(p_mem_node);
  433. return 2;
  434. }
  435. p_mem_node->next = ctrl->p_mem_head;
  436. ctrl->p_mem_head = p_mem_node;
  437. }
  438. while (numio--) {
  439. io_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  440. if (!io_node)
  441. break;
  442. io_node->base = *(u32*)p_byte;
  443. dbg("io base = %8.8x\n",io_node->base);
  444. p_byte += 4;
  445. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  446. kfree(io_node);
  447. return 2;
  448. }
  449. io_node->length = *(u32*)p_byte;
  450. dbg("io length = %8.8x\n",io_node->length);
  451. p_byte += 4;
  452. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  453. kfree(io_node);
  454. return 2;
  455. }
  456. io_node->next = ctrl->io_head;
  457. ctrl->io_head = io_node;
  458. }
  459. while (numbus--) {
  460. bus_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  461. if (!bus_node)
  462. break;
  463. bus_node->base = *(u32*)p_byte;
  464. p_byte += 4;
  465. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  466. kfree(bus_node);
  467. return 2;
  468. }
  469. bus_node->length = *(u32*)p_byte;
  470. p_byte += 4;
  471. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  472. kfree(bus_node);
  473. return 2;
  474. }
  475. bus_node->next = ctrl->bus_head;
  476. ctrl->bus_head = bus_node;
  477. }
  478. /* If all of the following fail, we don't have any resources for
  479. * hot plug add
  480. */
  481. rc = 1;
  482. rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  483. rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  484. rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  485. rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  486. if (rc)
  487. return(rc);
  488. } else {
  489. if ((evbuffer[0] != 0) && (!ctrl->push_flag))
  490. return 1;
  491. }
  492. return 0;
  493. }
  494. int compaq_nvram_store (void __iomem *rom_start)
  495. {
  496. int rc = 1;
  497. if (rom_start == NULL)
  498. return -ENODEV;
  499. if (evbuffer_init) {
  500. rc = store_HRT(rom_start);
  501. if (rc)
  502. err(msg_unable_to_save);
  503. }
  504. return rc;
  505. }