Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

mromprefix.S 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /*
  2. * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER )
  20. #define PCIBIOS_READ_CONFIG_WORD 0xb109
  21. #define PCIBIOS_READ_CONFIG_DWORD 0xb10a
  22. #define PCIBIOS_WRITE_CONFIG_WORD 0xb10c
  23. #define PCIBIOS_WRITE_CONFIG_DWORD 0xb10d
  24. #define PCI_COMMAND 0x04
  25. #define PCI_COMMAND_MEM 0x02
  26. #define PCI_BAR_0 0x10
  27. #define PCI_BAR_5 0x24
  28. #define PCI_BAR_EXPROM 0x30
  29. #define ROMPREFIX_EXCLUDE_PAYLOAD 1
  30. #define _rom_start _mrom_start
  31. #include "romprefix.S"
  32. .text
  33. .arch i386
  34. .code16
  35. /* Obtain access to payload by exposing the expansion ROM BAR at the
  36. * address currently used by a suitably large memory BAR on the same
  37. * device. The memory BAR is temporarily disabled. Using a memory
  38. * BAR on the same device means that we don't have to worry about the
  39. * configuration of any intermediate PCI bridges.
  40. *
  41. * Parameters:
  42. * %ds:0000 : Prefix
  43. * %esi : Buffer for copy of image source (or zero if no buffer available)
  44. * Returns:
  45. * %esi : Valid image source address (buffered or unbuffered)
  46. * CF set on error
  47. */
  48. .section ".text16.early", "awx", @progbits
  49. .globl open_payload
  50. open_payload:
  51. /* Preserve registers */
  52. pushl %eax
  53. pushw %bx
  54. pushl %ecx
  55. pushl %edx
  56. pushl %edi
  57. pushw %bp
  58. pushw %ds
  59. /* Retrieve bus:dev.fn and image source length from .prefix */
  60. movw init_pci_busdevfn, %bx
  61. movl image_source_len_dword, %ecx
  62. /* Set up %ds for access to .text16.early */
  63. pushw %cs
  64. popw %ds
  65. /* Store bus:dev.fn and image source length to .text16.early */
  66. movw %bx, payload_pci_busdevfn
  67. movl %ecx, rom_bar_copy_len_dword
  68. /* Get expansion ROM BAR current value */
  69. movw $PCI_BAR_EXPROM, %di
  70. call pci_read_bar
  71. movl %eax, rom_bar_orig_value
  72. /* Get expansion ROM BAR size */
  73. call pci_size_mem_bar_low
  74. movl %ecx, rom_bar_size
  75. /* Find a suitable memory BAR to use */
  76. movw $PCI_BAR_0, %di /* %di is PCI BAR register */
  77. xorw %bp, %bp /* %bp is increment */
  78. find_mem_bar:
  79. /* Move to next BAR */
  80. addw %bp, %di
  81. cmpw $PCI_BAR_5, %di
  82. jle 1f
  83. stc
  84. jmp 99f
  85. 1: movw $4, %bp
  86. /* Get BAR current value */
  87. call pci_read_bar
  88. /* Skip non-existent BARs */
  89. notl %eax
  90. testl %eax, %eax
  91. notl %eax
  92. jz find_mem_bar
  93. /* Skip I/O BARs */
  94. testb $0x01, %al
  95. jnz find_mem_bar
  96. /* Set increment to 8 for 64-bit BARs */
  97. testb $0x04, %al
  98. jz 1f
  99. movw $8, %bp
  100. 1:
  101. /* Skip 64-bit BARs with high dword set; we couldn't use this
  102. * address for the (32-bit) expansion ROM BAR anyway
  103. */
  104. testl %edx, %edx
  105. jnz find_mem_bar
  106. /* Get low dword of BAR size */
  107. call pci_size_mem_bar_low
  108. /* Skip BARs smaller than the expansion ROM BAR */
  109. cmpl %ecx, rom_bar_size
  110. ja find_mem_bar
  111. /* We have a memory BAR with a 32-bit address that is large
  112. * enough to use. Store BAR number and original value.
  113. */
  114. movw %di, stolen_bar_register
  115. movl %eax, stolen_bar_orig_value
  116. /* Remove flags from BAR address */
  117. xorb %al, %al
  118. /* Write zero to our stolen BAR. This doesn't technically
  119. * disable it, but it's a pretty safe bet that the PCI bridge
  120. * won't pass through accesses to this region anyway. Note
  121. * that the high dword (if any) must already be zero.
  122. */
  123. xorl %ecx, %ecx
  124. call pci_write_config_dword
  125. /* Enable expansion ROM BAR at stolen BAR's address */
  126. movl %eax, %ecx
  127. orb $0x1, %cl
  128. movw $PCI_BAR_EXPROM, %di
  129. call pci_write_config_dword
  130. /* Copy payload to buffer, or set buffer address to BAR address */
  131. testl %esi, %esi
  132. jz 1f
  133. /* We have a buffer; copy payload to it. Since .mrom is
  134. * designed specifically for real hardware, we assume that
  135. * flat real mode is working properly. (In the unlikely event
  136. * that this code is run inside a hypervisor that doesn't
  137. * properly support flat real mode, it will die horribly.)
  138. */
  139. pushl %esi
  140. pushw %es
  141. movl %esi, %edi
  142. movl %eax, %esi
  143. movl rom_bar_copy_len_dword, %ecx
  144. xorw %ax, %ax
  145. movw %ax, %es
  146. addr32 es rep movsl
  147. popw %es
  148. popl %esi
  149. jmp 2f
  150. 1: /* We have no buffer; set %esi to the BAR address */
  151. movl %eax, %esi
  152. 2:
  153. clc
  154. /* Restore registers and return */
  155. 99: popw %ds
  156. popw %bp
  157. popl %edi
  158. popl %edx
  159. popl %ecx
  160. popw %bx
  161. popl %eax
  162. lret
  163. .size open_payload, . - open_payload
  164. .section ".text16.early.data", "aw", @progbits
  165. payload_pci_busdevfn:
  166. .word 0
  167. .size payload_pci_busdevfn, . - payload_pci_busdevfn
  168. .section ".text16.early.data", "aw", @progbits
  169. rom_bar_orig_value:
  170. .long 0
  171. .size rom_bar_orig_value, . - rom_bar_orig_value
  172. .section ".text16.early.data", "aw", @progbits
  173. rom_bar_size:
  174. .long 0
  175. .size rom_bar_size, . - rom_bar_size
  176. .section ".text16.early.data", "aw", @progbits
  177. rom_bar_copy_len_dword:
  178. .long 0
  179. .size rom_bar_copy_len_dword, . - rom_bar_copy_len_dword
  180. .section ".text16.early.data", "aw", @progbits
  181. stolen_bar_register:
  182. .word 0
  183. .size stolen_bar_register, . - stolen_bar_register
  184. .section ".text16.early.data", "aw", @progbits
  185. stolen_bar_orig_value:
  186. .long 0
  187. .size stolen_bar_orig_value, . - stolen_bar_orig_value
  188. /* Restore original BAR values
  189. *
  190. * Parameters:
  191. * none
  192. * Returns:
  193. * none
  194. */
  195. .section ".text16.early", "awx", @progbits
  196. .globl close_payload
  197. close_payload:
  198. /* Preserve registers */
  199. pushw %bx
  200. pushw %di
  201. pushl %ecx
  202. pushw %ds
  203. /* Set up %ds for access to .text16.early */
  204. pushw %cs
  205. popw %ds
  206. /* Retrieve stored bus:dev.fn */
  207. movw payload_pci_busdevfn, %bx
  208. /* Restore expansion ROM BAR original value */
  209. movw $PCI_BAR_EXPROM, %di
  210. movl rom_bar_orig_value, %ecx
  211. call pci_write_config_dword
  212. /* Restore stolen BAR original value */
  213. movw stolen_bar_register, %di
  214. movl stolen_bar_orig_value, %ecx
  215. call pci_write_config_dword
  216. /* Restore registers and return */
  217. popw %ds
  218. popl %ecx
  219. popw %di
  220. popw %bx
  221. lret
  222. .size close_payload, . - close_payload
  223. /* Get PCI BAR value
  224. *
  225. * Parameters:
  226. * %bx : PCI bus:dev.fn
  227. * %di : PCI BAR register number
  228. * Returns:
  229. * %edx:%eax : PCI BAR value
  230. */
  231. .section ".text16.early", "awx", @progbits
  232. pci_read_bar:
  233. /* Preserve registers */
  234. pushl %ecx
  235. pushw %di
  236. /* Read low dword value */
  237. call pci_read_config_dword
  238. movl %ecx, %eax
  239. /* Read high dword value, if applicable */
  240. xorl %edx, %edx
  241. andb $0x07, %cl
  242. cmpb $0x04, %cl
  243. jne 1f
  244. addw $4, %di
  245. call pci_read_config_dword
  246. movl %ecx, %edx
  247. 1:
  248. /* Restore registers and return */
  249. popw %di
  250. popl %ecx
  251. ret
  252. .size pci_read_bar, . - pci_read_bar
  253. /* Get low dword of PCI memory BAR size
  254. *
  255. * Parameters:
  256. * %bx : PCI bus:dev.fn
  257. * %di : PCI BAR register number
  258. * %eax : Low dword of current PCI BAR value
  259. * Returns:
  260. * %ecx : PCI BAR size
  261. */
  262. .section ".text16.early", "awx", @progbits
  263. pci_size_mem_bar_low:
  264. /* Preserve registers */
  265. pushw %dx
  266. /* Disable memory accesses */
  267. xorw %dx, %dx
  268. call pci_set_mem_access
  269. /* Write all ones to BAR */
  270. xorl %ecx, %ecx
  271. decl %ecx
  272. call pci_write_config_dword
  273. /* Read back BAR */
  274. call pci_read_config_dword
  275. /* Calculate size */
  276. notl %ecx
  277. orb $0x0f, %cl
  278. incl %ecx
  279. /* Restore original value */
  280. pushl %ecx
  281. movl %eax, %ecx
  282. call pci_write_config_dword
  283. popl %ecx
  284. /* Enable memory accesses */
  285. movw $PCI_COMMAND_MEM, %dx
  286. call pci_set_mem_access
  287. /* Restore registers and return */
  288. popw %dx
  289. ret
  290. .size pci_size_mem_bar_low, . - pci_size_mem_bar_low
  291. /* Read PCI config dword
  292. *
  293. * Parameters:
  294. * %bx : PCI bus:dev.fn
  295. * %di : PCI register number
  296. * Returns:
  297. * %ecx : Dword value
  298. */
  299. .section ".text16.early", "awx", @progbits
  300. pci_read_config_dword:
  301. /* Preserve registers */
  302. pushl %eax
  303. pushl %ebx
  304. pushl %edx
  305. /* Issue INT 0x1a,b10a */
  306. movw $PCIBIOS_READ_CONFIG_DWORD, %ax
  307. int $0x1a
  308. /* Restore registers and return */
  309. popl %edx
  310. popl %ebx
  311. popl %eax
  312. ret
  313. .size pci_read_config_dword, . - pci_read_config_dword
  314. /* Write PCI config dword
  315. *
  316. * Parameters:
  317. * %bx : PCI bus:dev.fn
  318. * %di : PCI register number
  319. * %ecx : PCI BAR value
  320. * Returns:
  321. * none
  322. */
  323. .section ".text16.early", "awx", @progbits
  324. pci_write_config_dword:
  325. /* Preserve registers */
  326. pushal
  327. /* Issue INT 0x1a,b10d */
  328. movw $PCIBIOS_WRITE_CONFIG_DWORD, %ax
  329. int $0x1a
  330. /* Restore registers and return */
  331. popal
  332. ret
  333. .size pci_write_config_dword, . - pci_write_config_dword
  334. /* Enable/disable memory access response in PCI command word
  335. *
  336. * Parameters:
  337. * %bx : PCI bus:dev.fn
  338. * %dx : PCI_COMMAND_MEM, or zero
  339. * Returns:
  340. * none
  341. */
  342. .section ".text16.early", "awx", @progbits
  343. pci_set_mem_access:
  344. /* Preserve registers */
  345. pushal
  346. /* Read current value of command register */
  347. pushw %bx
  348. pushw %dx
  349. movw $PCI_COMMAND, %di
  350. movw $PCIBIOS_READ_CONFIG_WORD, %ax
  351. int $0x1a
  352. popw %dx
  353. popw %bx
  354. /* Set memory access enable as appropriate */
  355. andw $~PCI_COMMAND_MEM, %cx
  356. orw %dx, %cx
  357. /* Write new value of command register */
  358. movw $PCIBIOS_WRITE_CONFIG_WORD, %ax
  359. int $0x1a
  360. /* Restore registers and return */
  361. popal
  362. ret
  363. .size pci_set_mem_access, . - pci_set_mem_access
  364. /* Image source area length (in dwords)
  365. *
  366. */
  367. .section ".prefix", "ax", @progbits
  368. image_source_len_dword:
  369. .long 0
  370. .size image_source_len_dword, . - image_source_len_dword
  371. .section ".zinfo.fixup", "a", @progbits /* Compressor fixups */
  372. .ascii "ADDL"
  373. .long image_source_len_dword
  374. .long 4
  375. .long 0
  376. .previous