cpqphp_nvram.c 13 KB

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