cpqphp_nvram.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. }
  175. available = 1024;
  176. /* Now load the EV */
  177. temp_dword = available;
  178. rc = access_EV(READ_EV, "CQTHPS", evbuffer, &temp_dword);
  179. evbuffer_length = temp_dword;
  180. /* We're maintaining the resource lists so write FF to invalidate old
  181. * info
  182. */
  183. temp_dword = 1;
  184. rc = access_EV(WRITE_EV, "CQTHPS", &temp_byte, &temp_dword);
  185. return rc;
  186. }
  187. /*
  188. * store_HRT
  189. *
  190. * Save the hot plug Resource Table in NVRAM
  191. */
  192. static u32 store_HRT (void __iomem *rom_start)
  193. {
  194. u32 *buffer;
  195. u32 *pFill;
  196. u32 usedbytes;
  197. u32 available;
  198. u32 temp_dword;
  199. u32 rc;
  200. u8 loop;
  201. u8 numCtrl = 0;
  202. struct controller *ctrl;
  203. struct pci_resource *resNode;
  204. struct ev_hrt_header *p_EV_header;
  205. struct ev_hrt_ctrl *p_ev_ctrl;
  206. available = 1024;
  207. if (!check_for_compaq_ROM(rom_start)) {
  208. return(1);
  209. }
  210. buffer = (u32*) evbuffer;
  211. if (!buffer)
  212. return(1);
  213. pFill = buffer;
  214. usedbytes = 0;
  215. p_EV_header = (struct ev_hrt_header *) pFill;
  216. ctrl = cpqhp_ctrl_list;
  217. /* The revision of this structure */
  218. rc = add_byte(&pFill, 1 + ctrl->push_flag, &usedbytes, &available);
  219. if (rc)
  220. return(rc);
  221. /* The number of controllers */
  222. rc = add_byte(&pFill, 1, &usedbytes, &available);
  223. if (rc)
  224. return(rc);
  225. while (ctrl) {
  226. p_ev_ctrl = (struct ev_hrt_ctrl *) pFill;
  227. numCtrl++;
  228. /* The bus number */
  229. rc = add_byte(&pFill, ctrl->bus, &usedbytes, &available);
  230. if (rc)
  231. return(rc);
  232. /* The device Number */
  233. rc = add_byte(&pFill, PCI_SLOT(ctrl->pci_dev->devfn), &usedbytes, &available);
  234. if (rc)
  235. return(rc);
  236. /* The function Number */
  237. rc = add_byte(&pFill, PCI_FUNC(ctrl->pci_dev->devfn), &usedbytes, &available);
  238. if (rc)
  239. return(rc);
  240. /* Skip the number of available entries */
  241. rc = add_dword(&pFill, 0, &usedbytes, &available);
  242. if (rc)
  243. return(rc);
  244. /* Figure out memory Available */
  245. resNode = ctrl->mem_head;
  246. loop = 0;
  247. while (resNode) {
  248. loop ++;
  249. /* base */
  250. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  251. if (rc)
  252. return(rc);
  253. /* length */
  254. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  255. if (rc)
  256. return(rc);
  257. resNode = resNode->next;
  258. }
  259. /* Fill in the number of entries */
  260. p_ev_ctrl->mem_avail = loop;
  261. /* Figure out prefetchable memory Available */
  262. resNode = ctrl->p_mem_head;
  263. loop = 0;
  264. while (resNode) {
  265. loop ++;
  266. /* base */
  267. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  268. if (rc)
  269. return(rc);
  270. /* length */
  271. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  272. if (rc)
  273. return(rc);
  274. resNode = resNode->next;
  275. }
  276. /* Fill in the number of entries */
  277. p_ev_ctrl->p_mem_avail = loop;
  278. /* Figure out IO Available */
  279. resNode = ctrl->io_head;
  280. loop = 0;
  281. while (resNode) {
  282. loop ++;
  283. /* base */
  284. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  285. if (rc)
  286. return(rc);
  287. /* length */
  288. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  289. if (rc)
  290. return(rc);
  291. resNode = resNode->next;
  292. }
  293. /* Fill in the number of entries */
  294. p_ev_ctrl->io_avail = loop;
  295. /* Figure out bus Available */
  296. resNode = ctrl->bus_head;
  297. loop = 0;
  298. while (resNode) {
  299. loop ++;
  300. /* base */
  301. rc = add_dword(&pFill, resNode->base, &usedbytes, &available);
  302. if (rc)
  303. return(rc);
  304. /* length */
  305. rc = add_dword(&pFill, resNode->length, &usedbytes, &available);
  306. if (rc)
  307. return(rc);
  308. resNode = resNode->next;
  309. }
  310. /* Fill in the number of entries */
  311. p_ev_ctrl->bus_avail = loop;
  312. ctrl = ctrl->next;
  313. }
  314. p_EV_header->num_of_ctrl = numCtrl;
  315. /* Now store the EV */
  316. temp_dword = usedbytes;
  317. rc = access_EV(WRITE_EV, "CQTHPS", (u8*) buffer, &temp_dword);
  318. dbg("usedbytes = 0x%x, length = 0x%x\n", usedbytes, temp_dword);
  319. evbuffer_length = temp_dword;
  320. if (rc) {
  321. err(msg_unable_to_save);
  322. return(1);
  323. }
  324. return(0);
  325. }
  326. void compaq_nvram_init (void __iomem *rom_start)
  327. {
  328. if (rom_start) {
  329. compaq_int15_entry_point = (rom_start + ROM_INT15_PHY_ADDR - ROM_PHY_ADDR);
  330. }
  331. dbg("int15 entry = %p\n", compaq_int15_entry_point);
  332. /* initialize our int15 lock */
  333. spin_lock_init(&int15_lock);
  334. }
  335. int compaq_nvram_load (void __iomem *rom_start, struct controller *ctrl)
  336. {
  337. u8 bus, device, function;
  338. u8 nummem, numpmem, numio, numbus;
  339. u32 rc;
  340. u8 *p_byte;
  341. struct pci_resource *mem_node;
  342. struct pci_resource *p_mem_node;
  343. struct pci_resource *io_node;
  344. struct pci_resource *bus_node;
  345. struct ev_hrt_ctrl *p_ev_ctrl;
  346. struct ev_hrt_header *p_EV_header;
  347. if (!evbuffer_init) {
  348. /* Read the resource list information in from NVRAM */
  349. if (load_HRT(rom_start))
  350. memset (evbuffer, 0, 1024);
  351. evbuffer_init = 1;
  352. }
  353. /* If we saved information in NVRAM, use it now */
  354. p_EV_header = (struct ev_hrt_header *) evbuffer;
  355. /* The following code is for systems where version 1.0 of this
  356. * driver has been loaded, but doesn't support the hardware.
  357. * In that case, the driver would incorrectly store something
  358. * in NVRAM.
  359. */
  360. if ((p_EV_header->Version == 2) ||
  361. ((p_EV_header->Version == 1) && !ctrl->push_flag)) {
  362. p_byte = &(p_EV_header->next);
  363. p_ev_ctrl = (struct ev_hrt_ctrl *) &(p_EV_header->next);
  364. p_byte += 3;
  365. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  366. return 2;
  367. bus = p_ev_ctrl->bus;
  368. device = p_ev_ctrl->device;
  369. function = p_ev_ctrl->function;
  370. while ((bus != ctrl->bus) ||
  371. (device != PCI_SLOT(ctrl->pci_dev->devfn)) ||
  372. (function != PCI_FUNC(ctrl->pci_dev->devfn))) {
  373. nummem = p_ev_ctrl->mem_avail;
  374. numpmem = p_ev_ctrl->p_mem_avail;
  375. numio = p_ev_ctrl->io_avail;
  376. numbus = p_ev_ctrl->bus_avail;
  377. p_byte += 4;
  378. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  379. return 2;
  380. /* Skip forward to the next entry */
  381. p_byte += (nummem + numpmem + numio + numbus) * 8;
  382. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  383. return 2;
  384. p_ev_ctrl = (struct ev_hrt_ctrl *) p_byte;
  385. p_byte += 3;
  386. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  387. return 2;
  388. bus = p_ev_ctrl->bus;
  389. device = p_ev_ctrl->device;
  390. function = p_ev_ctrl->function;
  391. }
  392. nummem = p_ev_ctrl->mem_avail;
  393. numpmem = p_ev_ctrl->p_mem_avail;
  394. numio = p_ev_ctrl->io_avail;
  395. numbus = p_ev_ctrl->bus_avail;
  396. p_byte += 4;
  397. if (p_byte > ((u8*)p_EV_header + evbuffer_length))
  398. return 2;
  399. while (nummem--) {
  400. mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  401. if (!mem_node)
  402. break;
  403. mem_node->base = *(u32*)p_byte;
  404. dbg("mem base = %8.8x\n",mem_node->base);
  405. p_byte += 4;
  406. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  407. kfree(mem_node);
  408. return 2;
  409. }
  410. mem_node->length = *(u32*)p_byte;
  411. dbg("mem length = %8.8x\n",mem_node->length);
  412. p_byte += 4;
  413. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  414. kfree(mem_node);
  415. return 2;
  416. }
  417. mem_node->next = ctrl->mem_head;
  418. ctrl->mem_head = mem_node;
  419. }
  420. while (numpmem--) {
  421. p_mem_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  422. if (!p_mem_node)
  423. break;
  424. p_mem_node->base = *(u32*)p_byte;
  425. dbg("pre-mem base = %8.8x\n",p_mem_node->base);
  426. p_byte += 4;
  427. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  428. kfree(p_mem_node);
  429. return 2;
  430. }
  431. p_mem_node->length = *(u32*)p_byte;
  432. dbg("pre-mem length = %8.8x\n",p_mem_node->length);
  433. p_byte += 4;
  434. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  435. kfree(p_mem_node);
  436. return 2;
  437. }
  438. p_mem_node->next = ctrl->p_mem_head;
  439. ctrl->p_mem_head = p_mem_node;
  440. }
  441. while (numio--) {
  442. io_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  443. if (!io_node)
  444. break;
  445. io_node->base = *(u32*)p_byte;
  446. dbg("io base = %8.8x\n",io_node->base);
  447. p_byte += 4;
  448. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  449. kfree(io_node);
  450. return 2;
  451. }
  452. io_node->length = *(u32*)p_byte;
  453. dbg("io length = %8.8x\n",io_node->length);
  454. p_byte += 4;
  455. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  456. kfree(io_node);
  457. return 2;
  458. }
  459. io_node->next = ctrl->io_head;
  460. ctrl->io_head = io_node;
  461. }
  462. while (numbus--) {
  463. bus_node = kmalloc(sizeof(struct pci_resource), GFP_KERNEL);
  464. if (!bus_node)
  465. break;
  466. bus_node->base = *(u32*)p_byte;
  467. p_byte += 4;
  468. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  469. kfree(bus_node);
  470. return 2;
  471. }
  472. bus_node->length = *(u32*)p_byte;
  473. p_byte += 4;
  474. if (p_byte > ((u8*)p_EV_header + evbuffer_length)) {
  475. kfree(bus_node);
  476. return 2;
  477. }
  478. bus_node->next = ctrl->bus_head;
  479. ctrl->bus_head = bus_node;
  480. }
  481. /* If all of the following fail, we don't have any resources for
  482. * hot plug add
  483. */
  484. rc = 1;
  485. rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
  486. rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
  487. rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
  488. rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
  489. if (rc)
  490. return(rc);
  491. } else {
  492. if ((evbuffer[0] != 0) && (!ctrl->push_flag))
  493. return 1;
  494. }
  495. return 0;
  496. }
  497. int compaq_nvram_store (void __iomem *rom_start)
  498. {
  499. int rc = 1;
  500. if (rom_start == NULL)
  501. return -ENODEV;
  502. if (evbuffer_init) {
  503. rc = store_HRT(rom_start);
  504. if (rc) {
  505. err(msg_unable_to_save);
  506. }
  507. }
  508. return rc;
  509. }