You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mromprefix.S 9.3KB

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