amdgpu_atombios.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/amdgpu_drm.h>
  28. #include "amdgpu.h"
  29. #include "amdgpu_atombios.h"
  30. #include "amdgpu_atomfirmware.h"
  31. #include "amdgpu_i2c.h"
  32. #include "amdgpu_display.h"
  33. #include "atom.h"
  34. #include "atom-bits.h"
  35. #include "atombios_encoders.h"
  36. #include "bif/bif_4_1_d.h"
  37. static void amdgpu_atombios_lookup_i2c_gpio_quirks(struct amdgpu_device *adev,
  38. ATOM_GPIO_I2C_ASSIGMENT *gpio,
  39. u8 index)
  40. {
  41. }
  42. static struct amdgpu_i2c_bus_rec amdgpu_atombios_get_bus_rec_for_i2c_gpio(ATOM_GPIO_I2C_ASSIGMENT *gpio)
  43. {
  44. struct amdgpu_i2c_bus_rec i2c;
  45. memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
  46. i2c.mask_clk_reg = le16_to_cpu(gpio->usClkMaskRegisterIndex);
  47. i2c.mask_data_reg = le16_to_cpu(gpio->usDataMaskRegisterIndex);
  48. i2c.en_clk_reg = le16_to_cpu(gpio->usClkEnRegisterIndex);
  49. i2c.en_data_reg = le16_to_cpu(gpio->usDataEnRegisterIndex);
  50. i2c.y_clk_reg = le16_to_cpu(gpio->usClkY_RegisterIndex);
  51. i2c.y_data_reg = le16_to_cpu(gpio->usDataY_RegisterIndex);
  52. i2c.a_clk_reg = le16_to_cpu(gpio->usClkA_RegisterIndex);
  53. i2c.a_data_reg = le16_to_cpu(gpio->usDataA_RegisterIndex);
  54. i2c.mask_clk_mask = (1 << gpio->ucClkMaskShift);
  55. i2c.mask_data_mask = (1 << gpio->ucDataMaskShift);
  56. i2c.en_clk_mask = (1 << gpio->ucClkEnShift);
  57. i2c.en_data_mask = (1 << gpio->ucDataEnShift);
  58. i2c.y_clk_mask = (1 << gpio->ucClkY_Shift);
  59. i2c.y_data_mask = (1 << gpio->ucDataY_Shift);
  60. i2c.a_clk_mask = (1 << gpio->ucClkA_Shift);
  61. i2c.a_data_mask = (1 << gpio->ucDataA_Shift);
  62. if (gpio->sucI2cId.sbfAccess.bfHW_Capable)
  63. i2c.hw_capable = true;
  64. else
  65. i2c.hw_capable = false;
  66. if (gpio->sucI2cId.ucAccess == 0xa0)
  67. i2c.mm_i2c = true;
  68. else
  69. i2c.mm_i2c = false;
  70. i2c.i2c_id = gpio->sucI2cId.ucAccess;
  71. if (i2c.mask_clk_reg)
  72. i2c.valid = true;
  73. else
  74. i2c.valid = false;
  75. return i2c;
  76. }
  77. struct amdgpu_i2c_bus_rec amdgpu_atombios_lookup_i2c_gpio(struct amdgpu_device *adev,
  78. uint8_t id)
  79. {
  80. struct atom_context *ctx = adev->mode_info.atom_context;
  81. ATOM_GPIO_I2C_ASSIGMENT *gpio;
  82. struct amdgpu_i2c_bus_rec i2c;
  83. int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
  84. struct _ATOM_GPIO_I2C_INFO *i2c_info;
  85. uint16_t data_offset, size;
  86. int i, num_indices;
  87. memset(&i2c, 0, sizeof(struct amdgpu_i2c_bus_rec));
  88. i2c.valid = false;
  89. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  90. i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
  91. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  92. sizeof(ATOM_GPIO_I2C_ASSIGMENT);
  93. gpio = &i2c_info->asGPIO_Info[0];
  94. for (i = 0; i < num_indices; i++) {
  95. amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
  96. if (gpio->sucI2cId.ucAccess == id) {
  97. i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
  98. break;
  99. }
  100. gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
  101. ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
  102. }
  103. }
  104. return i2c;
  105. }
  106. void amdgpu_atombios_i2c_init(struct amdgpu_device *adev)
  107. {
  108. struct atom_context *ctx = adev->mode_info.atom_context;
  109. ATOM_GPIO_I2C_ASSIGMENT *gpio;
  110. struct amdgpu_i2c_bus_rec i2c;
  111. int index = GetIndexIntoMasterTable(DATA, GPIO_I2C_Info);
  112. struct _ATOM_GPIO_I2C_INFO *i2c_info;
  113. uint16_t data_offset, size;
  114. int i, num_indices;
  115. char stmp[32];
  116. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  117. i2c_info = (struct _ATOM_GPIO_I2C_INFO *)(ctx->bios + data_offset);
  118. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  119. sizeof(ATOM_GPIO_I2C_ASSIGMENT);
  120. gpio = &i2c_info->asGPIO_Info[0];
  121. for (i = 0; i < num_indices; i++) {
  122. amdgpu_atombios_lookup_i2c_gpio_quirks(adev, gpio, i);
  123. i2c = amdgpu_atombios_get_bus_rec_for_i2c_gpio(gpio);
  124. if (i2c.valid) {
  125. sprintf(stmp, "0x%x", i2c.i2c_id);
  126. adev->i2c_bus[i] = amdgpu_i2c_create(adev->ddev, &i2c, stmp);
  127. }
  128. gpio = (ATOM_GPIO_I2C_ASSIGMENT *)
  129. ((u8 *)gpio + sizeof(ATOM_GPIO_I2C_ASSIGMENT));
  130. }
  131. }
  132. }
  133. struct amdgpu_gpio_rec
  134. amdgpu_atombios_lookup_gpio(struct amdgpu_device *adev,
  135. u8 id)
  136. {
  137. struct atom_context *ctx = adev->mode_info.atom_context;
  138. struct amdgpu_gpio_rec gpio;
  139. int index = GetIndexIntoMasterTable(DATA, GPIO_Pin_LUT);
  140. struct _ATOM_GPIO_PIN_LUT *gpio_info;
  141. ATOM_GPIO_PIN_ASSIGNMENT *pin;
  142. u16 data_offset, size;
  143. int i, num_indices;
  144. memset(&gpio, 0, sizeof(struct amdgpu_gpio_rec));
  145. gpio.valid = false;
  146. if (amdgpu_atom_parse_data_header(ctx, index, &size, NULL, NULL, &data_offset)) {
  147. gpio_info = (struct _ATOM_GPIO_PIN_LUT *)(ctx->bios + data_offset);
  148. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  149. sizeof(ATOM_GPIO_PIN_ASSIGNMENT);
  150. pin = gpio_info->asGPIO_Pin;
  151. for (i = 0; i < num_indices; i++) {
  152. if (id == pin->ucGPIO_ID) {
  153. gpio.id = pin->ucGPIO_ID;
  154. gpio.reg = le16_to_cpu(pin->usGpioPin_AIndex);
  155. gpio.shift = pin->ucGpioPinBitShift;
  156. gpio.mask = (1 << pin->ucGpioPinBitShift);
  157. gpio.valid = true;
  158. break;
  159. }
  160. pin = (ATOM_GPIO_PIN_ASSIGNMENT *)
  161. ((u8 *)pin + sizeof(ATOM_GPIO_PIN_ASSIGNMENT));
  162. }
  163. }
  164. return gpio;
  165. }
  166. static struct amdgpu_hpd
  167. amdgpu_atombios_get_hpd_info_from_gpio(struct amdgpu_device *adev,
  168. struct amdgpu_gpio_rec *gpio)
  169. {
  170. struct amdgpu_hpd hpd;
  171. u32 reg;
  172. memset(&hpd, 0, sizeof(struct amdgpu_hpd));
  173. reg = amdgpu_display_hpd_get_gpio_reg(adev);
  174. hpd.gpio = *gpio;
  175. if (gpio->reg == reg) {
  176. switch(gpio->mask) {
  177. case (1 << 0):
  178. hpd.hpd = AMDGPU_HPD_1;
  179. break;
  180. case (1 << 8):
  181. hpd.hpd = AMDGPU_HPD_2;
  182. break;
  183. case (1 << 16):
  184. hpd.hpd = AMDGPU_HPD_3;
  185. break;
  186. case (1 << 24):
  187. hpd.hpd = AMDGPU_HPD_4;
  188. break;
  189. case (1 << 26):
  190. hpd.hpd = AMDGPU_HPD_5;
  191. break;
  192. case (1 << 28):
  193. hpd.hpd = AMDGPU_HPD_6;
  194. break;
  195. default:
  196. hpd.hpd = AMDGPU_HPD_NONE;
  197. break;
  198. }
  199. } else
  200. hpd.hpd = AMDGPU_HPD_NONE;
  201. return hpd;
  202. }
  203. static const int object_connector_convert[] = {
  204. DRM_MODE_CONNECTOR_Unknown,
  205. DRM_MODE_CONNECTOR_DVII,
  206. DRM_MODE_CONNECTOR_DVII,
  207. DRM_MODE_CONNECTOR_DVID,
  208. DRM_MODE_CONNECTOR_DVID,
  209. DRM_MODE_CONNECTOR_VGA,
  210. DRM_MODE_CONNECTOR_Composite,
  211. DRM_MODE_CONNECTOR_SVIDEO,
  212. DRM_MODE_CONNECTOR_Unknown,
  213. DRM_MODE_CONNECTOR_Unknown,
  214. DRM_MODE_CONNECTOR_9PinDIN,
  215. DRM_MODE_CONNECTOR_Unknown,
  216. DRM_MODE_CONNECTOR_HDMIA,
  217. DRM_MODE_CONNECTOR_HDMIB,
  218. DRM_MODE_CONNECTOR_LVDS,
  219. DRM_MODE_CONNECTOR_9PinDIN,
  220. DRM_MODE_CONNECTOR_Unknown,
  221. DRM_MODE_CONNECTOR_Unknown,
  222. DRM_MODE_CONNECTOR_Unknown,
  223. DRM_MODE_CONNECTOR_DisplayPort,
  224. DRM_MODE_CONNECTOR_eDP,
  225. DRM_MODE_CONNECTOR_Unknown
  226. };
  227. bool amdgpu_atombios_has_dce_engine_info(struct amdgpu_device *adev)
  228. {
  229. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  230. struct atom_context *ctx = mode_info->atom_context;
  231. int index = GetIndexIntoMasterTable(DATA, Object_Header);
  232. u16 size, data_offset;
  233. u8 frev, crev;
  234. ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
  235. ATOM_OBJECT_HEADER *obj_header;
  236. if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
  237. return false;
  238. if (crev < 2)
  239. return false;
  240. obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
  241. path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
  242. (ctx->bios + data_offset +
  243. le16_to_cpu(obj_header->usDisplayPathTableOffset));
  244. if (path_obj->ucNumOfDispPath)
  245. return true;
  246. else
  247. return false;
  248. }
  249. bool amdgpu_atombios_get_connector_info_from_object_table(struct amdgpu_device *adev)
  250. {
  251. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  252. struct atom_context *ctx = mode_info->atom_context;
  253. int index = GetIndexIntoMasterTable(DATA, Object_Header);
  254. u16 size, data_offset;
  255. u8 frev, crev;
  256. ATOM_CONNECTOR_OBJECT_TABLE *con_obj;
  257. ATOM_ENCODER_OBJECT_TABLE *enc_obj;
  258. ATOM_OBJECT_TABLE *router_obj;
  259. ATOM_DISPLAY_OBJECT_PATH_TABLE *path_obj;
  260. ATOM_OBJECT_HEADER *obj_header;
  261. int i, j, k, path_size, device_support;
  262. int connector_type;
  263. u16 conn_id, connector_object_id;
  264. struct amdgpu_i2c_bus_rec ddc_bus;
  265. struct amdgpu_router router;
  266. struct amdgpu_gpio_rec gpio;
  267. struct amdgpu_hpd hpd;
  268. if (!amdgpu_atom_parse_data_header(ctx, index, &size, &frev, &crev, &data_offset))
  269. return false;
  270. if (crev < 2)
  271. return false;
  272. obj_header = (ATOM_OBJECT_HEADER *) (ctx->bios + data_offset);
  273. path_obj = (ATOM_DISPLAY_OBJECT_PATH_TABLE *)
  274. (ctx->bios + data_offset +
  275. le16_to_cpu(obj_header->usDisplayPathTableOffset));
  276. con_obj = (ATOM_CONNECTOR_OBJECT_TABLE *)
  277. (ctx->bios + data_offset +
  278. le16_to_cpu(obj_header->usConnectorObjectTableOffset));
  279. enc_obj = (ATOM_ENCODER_OBJECT_TABLE *)
  280. (ctx->bios + data_offset +
  281. le16_to_cpu(obj_header->usEncoderObjectTableOffset));
  282. router_obj = (ATOM_OBJECT_TABLE *)
  283. (ctx->bios + data_offset +
  284. le16_to_cpu(obj_header->usRouterObjectTableOffset));
  285. device_support = le16_to_cpu(obj_header->usDeviceSupport);
  286. path_size = 0;
  287. for (i = 0; i < path_obj->ucNumOfDispPath; i++) {
  288. uint8_t *addr = (uint8_t *) path_obj->asDispPath;
  289. ATOM_DISPLAY_OBJECT_PATH *path;
  290. addr += path_size;
  291. path = (ATOM_DISPLAY_OBJECT_PATH *) addr;
  292. path_size += le16_to_cpu(path->usSize);
  293. if (device_support & le16_to_cpu(path->usDeviceTag)) {
  294. uint8_t con_obj_id, con_obj_num, con_obj_type;
  295. con_obj_id =
  296. (le16_to_cpu(path->usConnObjectId) & OBJECT_ID_MASK)
  297. >> OBJECT_ID_SHIFT;
  298. con_obj_num =
  299. (le16_to_cpu(path->usConnObjectId) & ENUM_ID_MASK)
  300. >> ENUM_ID_SHIFT;
  301. con_obj_type =
  302. (le16_to_cpu(path->usConnObjectId) &
  303. OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
  304. /* Skip TV/CV support */
  305. if ((le16_to_cpu(path->usDeviceTag) ==
  306. ATOM_DEVICE_TV1_SUPPORT) ||
  307. (le16_to_cpu(path->usDeviceTag) ==
  308. ATOM_DEVICE_CV_SUPPORT))
  309. continue;
  310. if (con_obj_id >= ARRAY_SIZE(object_connector_convert)) {
  311. DRM_ERROR("invalid con_obj_id %d for device tag 0x%04x\n",
  312. con_obj_id, le16_to_cpu(path->usDeviceTag));
  313. continue;
  314. }
  315. connector_type =
  316. object_connector_convert[con_obj_id];
  317. connector_object_id = con_obj_id;
  318. if (connector_type == DRM_MODE_CONNECTOR_Unknown)
  319. continue;
  320. router.ddc_valid = false;
  321. router.cd_valid = false;
  322. for (j = 0; j < ((le16_to_cpu(path->usSize) - 8) / 2); j++) {
  323. uint8_t grph_obj_id, grph_obj_num, grph_obj_type;
  324. grph_obj_id =
  325. (le16_to_cpu(path->usGraphicObjIds[j]) &
  326. OBJECT_ID_MASK) >> OBJECT_ID_SHIFT;
  327. grph_obj_num =
  328. (le16_to_cpu(path->usGraphicObjIds[j]) &
  329. ENUM_ID_MASK) >> ENUM_ID_SHIFT;
  330. grph_obj_type =
  331. (le16_to_cpu(path->usGraphicObjIds[j]) &
  332. OBJECT_TYPE_MASK) >> OBJECT_TYPE_SHIFT;
  333. if (grph_obj_type == GRAPH_OBJECT_TYPE_ENCODER) {
  334. for (k = 0; k < enc_obj->ucNumberOfObjects; k++) {
  335. u16 encoder_obj = le16_to_cpu(enc_obj->asObjects[k].usObjectID);
  336. if (le16_to_cpu(path->usGraphicObjIds[j]) == encoder_obj) {
  337. ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
  338. (ctx->bios + data_offset +
  339. le16_to_cpu(enc_obj->asObjects[k].usRecordOffset));
  340. ATOM_ENCODER_CAP_RECORD *cap_record;
  341. u16 caps = 0;
  342. while (record->ucRecordSize > 0 &&
  343. record->ucRecordType > 0 &&
  344. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  345. switch (record->ucRecordType) {
  346. case ATOM_ENCODER_CAP_RECORD_TYPE:
  347. cap_record =(ATOM_ENCODER_CAP_RECORD *)
  348. record;
  349. caps = le16_to_cpu(cap_record->usEncoderCap);
  350. break;
  351. }
  352. record = (ATOM_COMMON_RECORD_HEADER *)
  353. ((char *)record + record->ucRecordSize);
  354. }
  355. amdgpu_display_add_encoder(adev, encoder_obj,
  356. le16_to_cpu(path->usDeviceTag),
  357. caps);
  358. }
  359. }
  360. } else if (grph_obj_type == GRAPH_OBJECT_TYPE_ROUTER) {
  361. for (k = 0; k < router_obj->ucNumberOfObjects; k++) {
  362. u16 router_obj_id = le16_to_cpu(router_obj->asObjects[k].usObjectID);
  363. if (le16_to_cpu(path->usGraphicObjIds[j]) == router_obj_id) {
  364. ATOM_COMMON_RECORD_HEADER *record = (ATOM_COMMON_RECORD_HEADER *)
  365. (ctx->bios + data_offset +
  366. le16_to_cpu(router_obj->asObjects[k].usRecordOffset));
  367. ATOM_I2C_RECORD *i2c_record;
  368. ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
  369. ATOM_ROUTER_DDC_PATH_SELECT_RECORD *ddc_path;
  370. ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *cd_path;
  371. ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *router_src_dst_table =
  372. (ATOM_SRC_DST_TABLE_FOR_ONE_OBJECT *)
  373. (ctx->bios + data_offset +
  374. le16_to_cpu(router_obj->asObjects[k].usSrcDstTableOffset));
  375. u8 *num_dst_objs = (u8 *)
  376. ((u8 *)router_src_dst_table + 1 +
  377. (router_src_dst_table->ucNumberOfSrc * 2));
  378. u16 *dst_objs = (u16 *)(num_dst_objs + 1);
  379. int enum_id;
  380. router.router_id = router_obj_id;
  381. for (enum_id = 0; enum_id < (*num_dst_objs); enum_id++) {
  382. if (le16_to_cpu(path->usConnObjectId) ==
  383. le16_to_cpu(dst_objs[enum_id]))
  384. break;
  385. }
  386. while (record->ucRecordSize > 0 &&
  387. record->ucRecordType > 0 &&
  388. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  389. switch (record->ucRecordType) {
  390. case ATOM_I2C_RECORD_TYPE:
  391. i2c_record =
  392. (ATOM_I2C_RECORD *)
  393. record;
  394. i2c_config =
  395. (ATOM_I2C_ID_CONFIG_ACCESS *)
  396. &i2c_record->sucI2cId;
  397. router.i2c_info =
  398. amdgpu_atombios_lookup_i2c_gpio(adev,
  399. i2c_config->
  400. ucAccess);
  401. router.i2c_addr = i2c_record->ucI2CAddr >> 1;
  402. break;
  403. case ATOM_ROUTER_DDC_PATH_SELECT_RECORD_TYPE:
  404. ddc_path = (ATOM_ROUTER_DDC_PATH_SELECT_RECORD *)
  405. record;
  406. router.ddc_valid = true;
  407. router.ddc_mux_type = ddc_path->ucMuxType;
  408. router.ddc_mux_control_pin = ddc_path->ucMuxControlPin;
  409. router.ddc_mux_state = ddc_path->ucMuxState[enum_id];
  410. break;
  411. case ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD_TYPE:
  412. cd_path = (ATOM_ROUTER_DATA_CLOCK_PATH_SELECT_RECORD *)
  413. record;
  414. router.cd_valid = true;
  415. router.cd_mux_type = cd_path->ucMuxType;
  416. router.cd_mux_control_pin = cd_path->ucMuxControlPin;
  417. router.cd_mux_state = cd_path->ucMuxState[enum_id];
  418. break;
  419. }
  420. record = (ATOM_COMMON_RECORD_HEADER *)
  421. ((char *)record + record->ucRecordSize);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. /* look up gpio for ddc, hpd */
  428. ddc_bus.valid = false;
  429. hpd.hpd = AMDGPU_HPD_NONE;
  430. if ((le16_to_cpu(path->usDeviceTag) &
  431. (ATOM_DEVICE_TV_SUPPORT | ATOM_DEVICE_CV_SUPPORT)) == 0) {
  432. for (j = 0; j < con_obj->ucNumberOfObjects; j++) {
  433. if (le16_to_cpu(path->usConnObjectId) ==
  434. le16_to_cpu(con_obj->asObjects[j].
  435. usObjectID)) {
  436. ATOM_COMMON_RECORD_HEADER
  437. *record =
  438. (ATOM_COMMON_RECORD_HEADER
  439. *)
  440. (ctx->bios + data_offset +
  441. le16_to_cpu(con_obj->
  442. asObjects[j].
  443. usRecordOffset));
  444. ATOM_I2C_RECORD *i2c_record;
  445. ATOM_HPD_INT_RECORD *hpd_record;
  446. ATOM_I2C_ID_CONFIG_ACCESS *i2c_config;
  447. while (record->ucRecordSize > 0 &&
  448. record->ucRecordType > 0 &&
  449. record->ucRecordType <= ATOM_MAX_OBJECT_RECORD_NUMBER) {
  450. switch (record->ucRecordType) {
  451. case ATOM_I2C_RECORD_TYPE:
  452. i2c_record =
  453. (ATOM_I2C_RECORD *)
  454. record;
  455. i2c_config =
  456. (ATOM_I2C_ID_CONFIG_ACCESS *)
  457. &i2c_record->sucI2cId;
  458. ddc_bus = amdgpu_atombios_lookup_i2c_gpio(adev,
  459. i2c_config->
  460. ucAccess);
  461. break;
  462. case ATOM_HPD_INT_RECORD_TYPE:
  463. hpd_record =
  464. (ATOM_HPD_INT_RECORD *)
  465. record;
  466. gpio = amdgpu_atombios_lookup_gpio(adev,
  467. hpd_record->ucHPDIntGPIOID);
  468. hpd = amdgpu_atombios_get_hpd_info_from_gpio(adev, &gpio);
  469. hpd.plugged_state = hpd_record->ucPlugged_PinState;
  470. break;
  471. }
  472. record =
  473. (ATOM_COMMON_RECORD_HEADER
  474. *) ((char *)record
  475. +
  476. record->
  477. ucRecordSize);
  478. }
  479. break;
  480. }
  481. }
  482. }
  483. /* needed for aux chan transactions */
  484. ddc_bus.hpd = hpd.hpd;
  485. conn_id = le16_to_cpu(path->usConnObjectId);
  486. amdgpu_display_add_connector(adev,
  487. conn_id,
  488. le16_to_cpu(path->usDeviceTag),
  489. connector_type, &ddc_bus,
  490. connector_object_id,
  491. &hpd,
  492. &router);
  493. }
  494. }
  495. amdgpu_link_encoder_connector(adev->ddev);
  496. return true;
  497. }
  498. union firmware_info {
  499. ATOM_FIRMWARE_INFO info;
  500. ATOM_FIRMWARE_INFO_V1_2 info_12;
  501. ATOM_FIRMWARE_INFO_V1_3 info_13;
  502. ATOM_FIRMWARE_INFO_V1_4 info_14;
  503. ATOM_FIRMWARE_INFO_V2_1 info_21;
  504. ATOM_FIRMWARE_INFO_V2_2 info_22;
  505. };
  506. int amdgpu_atombios_get_clock_info(struct amdgpu_device *adev)
  507. {
  508. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  509. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  510. uint8_t frev, crev;
  511. uint16_t data_offset;
  512. int ret = -EINVAL;
  513. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  514. &frev, &crev, &data_offset)) {
  515. int i;
  516. struct amdgpu_pll *ppll = &adev->clock.ppll[0];
  517. struct amdgpu_pll *spll = &adev->clock.spll;
  518. struct amdgpu_pll *mpll = &adev->clock.mpll;
  519. union firmware_info *firmware_info =
  520. (union firmware_info *)(mode_info->atom_context->bios +
  521. data_offset);
  522. /* pixel clocks */
  523. ppll->reference_freq =
  524. le16_to_cpu(firmware_info->info.usReferenceClock);
  525. ppll->reference_div = 0;
  526. ppll->pll_out_min =
  527. le32_to_cpu(firmware_info->info_12.ulMinPixelClockPLL_Output);
  528. ppll->pll_out_max =
  529. le32_to_cpu(firmware_info->info.ulMaxPixelClockPLL_Output);
  530. ppll->lcd_pll_out_min =
  531. le16_to_cpu(firmware_info->info_14.usLcdMinPixelClockPLL_Output) * 100;
  532. if (ppll->lcd_pll_out_min == 0)
  533. ppll->lcd_pll_out_min = ppll->pll_out_min;
  534. ppll->lcd_pll_out_max =
  535. le16_to_cpu(firmware_info->info_14.usLcdMaxPixelClockPLL_Output) * 100;
  536. if (ppll->lcd_pll_out_max == 0)
  537. ppll->lcd_pll_out_max = ppll->pll_out_max;
  538. if (ppll->pll_out_min == 0)
  539. ppll->pll_out_min = 64800;
  540. ppll->pll_in_min =
  541. le16_to_cpu(firmware_info->info.usMinPixelClockPLL_Input);
  542. ppll->pll_in_max =
  543. le16_to_cpu(firmware_info->info.usMaxPixelClockPLL_Input);
  544. ppll->min_post_div = 2;
  545. ppll->max_post_div = 0x7f;
  546. ppll->min_frac_feedback_div = 0;
  547. ppll->max_frac_feedback_div = 9;
  548. ppll->min_ref_div = 2;
  549. ppll->max_ref_div = 0x3ff;
  550. ppll->min_feedback_div = 4;
  551. ppll->max_feedback_div = 0xfff;
  552. ppll->best_vco = 0;
  553. for (i = 1; i < AMDGPU_MAX_PPLL; i++)
  554. adev->clock.ppll[i] = *ppll;
  555. /* system clock */
  556. spll->reference_freq =
  557. le16_to_cpu(firmware_info->info_21.usCoreReferenceClock);
  558. spll->reference_div = 0;
  559. spll->pll_out_min =
  560. le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Output);
  561. spll->pll_out_max =
  562. le32_to_cpu(firmware_info->info.ulMaxEngineClockPLL_Output);
  563. /* ??? */
  564. if (spll->pll_out_min == 0)
  565. spll->pll_out_min = 64800;
  566. spll->pll_in_min =
  567. le16_to_cpu(firmware_info->info.usMinEngineClockPLL_Input);
  568. spll->pll_in_max =
  569. le16_to_cpu(firmware_info->info.usMaxEngineClockPLL_Input);
  570. spll->min_post_div = 1;
  571. spll->max_post_div = 1;
  572. spll->min_ref_div = 2;
  573. spll->max_ref_div = 0xff;
  574. spll->min_feedback_div = 4;
  575. spll->max_feedback_div = 0xff;
  576. spll->best_vco = 0;
  577. /* memory clock */
  578. mpll->reference_freq =
  579. le16_to_cpu(firmware_info->info_21.usMemoryReferenceClock);
  580. mpll->reference_div = 0;
  581. mpll->pll_out_min =
  582. le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Output);
  583. mpll->pll_out_max =
  584. le32_to_cpu(firmware_info->info.ulMaxMemoryClockPLL_Output);
  585. /* ??? */
  586. if (mpll->pll_out_min == 0)
  587. mpll->pll_out_min = 64800;
  588. mpll->pll_in_min =
  589. le16_to_cpu(firmware_info->info.usMinMemoryClockPLL_Input);
  590. mpll->pll_in_max =
  591. le16_to_cpu(firmware_info->info.usMaxMemoryClockPLL_Input);
  592. adev->clock.default_sclk =
  593. le32_to_cpu(firmware_info->info.ulDefaultEngineClock);
  594. adev->clock.default_mclk =
  595. le32_to_cpu(firmware_info->info.ulDefaultMemoryClock);
  596. mpll->min_post_div = 1;
  597. mpll->max_post_div = 1;
  598. mpll->min_ref_div = 2;
  599. mpll->max_ref_div = 0xff;
  600. mpll->min_feedback_div = 4;
  601. mpll->max_feedback_div = 0xff;
  602. mpll->best_vco = 0;
  603. /* disp clock */
  604. adev->clock.default_dispclk =
  605. le32_to_cpu(firmware_info->info_21.ulDefaultDispEngineClkFreq);
  606. /* set a reasonable default for DP */
  607. if (adev->clock.default_dispclk < 53900) {
  608. DRM_DEBUG("Changing default dispclk from %dMhz to 600Mhz\n",
  609. adev->clock.default_dispclk / 100);
  610. adev->clock.default_dispclk = 60000;
  611. } else if (adev->clock.default_dispclk <= 60000) {
  612. DRM_DEBUG("Changing default dispclk from %dMhz to 625Mhz\n",
  613. adev->clock.default_dispclk / 100);
  614. adev->clock.default_dispclk = 62500;
  615. }
  616. adev->clock.dp_extclk =
  617. le16_to_cpu(firmware_info->info_21.usUniphyDPModeExtClkFreq);
  618. adev->clock.current_dispclk = adev->clock.default_dispclk;
  619. adev->clock.max_pixel_clock = le16_to_cpu(firmware_info->info.usMaxPixelClock);
  620. if (adev->clock.max_pixel_clock == 0)
  621. adev->clock.max_pixel_clock = 40000;
  622. /* not technically a clock, but... */
  623. adev->mode_info.firmware_flags =
  624. le16_to_cpu(firmware_info->info.usFirmwareCapability.susAccess);
  625. ret = 0;
  626. }
  627. adev->pm.current_sclk = adev->clock.default_sclk;
  628. adev->pm.current_mclk = adev->clock.default_mclk;
  629. return ret;
  630. }
  631. union gfx_info {
  632. ATOM_GFX_INFO_V2_1 info;
  633. };
  634. int amdgpu_atombios_get_gfx_info(struct amdgpu_device *adev)
  635. {
  636. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  637. int index = GetIndexIntoMasterTable(DATA, GFX_Info);
  638. uint8_t frev, crev;
  639. uint16_t data_offset;
  640. int ret = -EINVAL;
  641. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  642. &frev, &crev, &data_offset)) {
  643. union gfx_info *gfx_info = (union gfx_info *)
  644. (mode_info->atom_context->bios + data_offset);
  645. adev->gfx.config.max_shader_engines = gfx_info->info.max_shader_engines;
  646. adev->gfx.config.max_tile_pipes = gfx_info->info.max_tile_pipes;
  647. adev->gfx.config.max_cu_per_sh = gfx_info->info.max_cu_per_sh;
  648. adev->gfx.config.max_sh_per_se = gfx_info->info.max_sh_per_se;
  649. adev->gfx.config.max_backends_per_se = gfx_info->info.max_backends_per_se;
  650. adev->gfx.config.max_texture_channel_caches =
  651. gfx_info->info.max_texture_channel_caches;
  652. ret = 0;
  653. }
  654. return ret;
  655. }
  656. union igp_info {
  657. struct _ATOM_INTEGRATED_SYSTEM_INFO info;
  658. struct _ATOM_INTEGRATED_SYSTEM_INFO_V2 info_2;
  659. struct _ATOM_INTEGRATED_SYSTEM_INFO_V6 info_6;
  660. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_7 info_7;
  661. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_8 info_8;
  662. struct _ATOM_INTEGRATED_SYSTEM_INFO_V1_9 info_9;
  663. };
  664. /*
  665. * Return vram width from integrated system info table, if available,
  666. * or 0 if not.
  667. */
  668. int amdgpu_atombios_get_vram_width(struct amdgpu_device *adev)
  669. {
  670. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  671. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  672. u16 data_offset, size;
  673. union igp_info *igp_info;
  674. u8 frev, crev;
  675. /* get any igp specific overrides */
  676. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  677. &frev, &crev, &data_offset)) {
  678. igp_info = (union igp_info *)
  679. (mode_info->atom_context->bios + data_offset);
  680. switch (crev) {
  681. case 8:
  682. case 9:
  683. return igp_info->info_8.ucUMAChannelNumber * 64;
  684. default:
  685. return 0;
  686. }
  687. }
  688. return 0;
  689. }
  690. static void amdgpu_atombios_get_igp_ss_overrides(struct amdgpu_device *adev,
  691. struct amdgpu_atom_ss *ss,
  692. int id)
  693. {
  694. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  695. int index = GetIndexIntoMasterTable(DATA, IntegratedSystemInfo);
  696. u16 data_offset, size;
  697. union igp_info *igp_info;
  698. u8 frev, crev;
  699. u16 percentage = 0, rate = 0;
  700. /* get any igp specific overrides */
  701. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  702. &frev, &crev, &data_offset)) {
  703. igp_info = (union igp_info *)
  704. (mode_info->atom_context->bios + data_offset);
  705. switch (crev) {
  706. case 6:
  707. switch (id) {
  708. case ASIC_INTERNAL_SS_ON_TMDS:
  709. percentage = le16_to_cpu(igp_info->info_6.usDVISSPercentage);
  710. rate = le16_to_cpu(igp_info->info_6.usDVISSpreadRateIn10Hz);
  711. break;
  712. case ASIC_INTERNAL_SS_ON_HDMI:
  713. percentage = le16_to_cpu(igp_info->info_6.usHDMISSPercentage);
  714. rate = le16_to_cpu(igp_info->info_6.usHDMISSpreadRateIn10Hz);
  715. break;
  716. case ASIC_INTERNAL_SS_ON_LVDS:
  717. percentage = le16_to_cpu(igp_info->info_6.usLvdsSSPercentage);
  718. rate = le16_to_cpu(igp_info->info_6.usLvdsSSpreadRateIn10Hz);
  719. break;
  720. }
  721. break;
  722. case 7:
  723. switch (id) {
  724. case ASIC_INTERNAL_SS_ON_TMDS:
  725. percentage = le16_to_cpu(igp_info->info_7.usDVISSPercentage);
  726. rate = le16_to_cpu(igp_info->info_7.usDVISSpreadRateIn10Hz);
  727. break;
  728. case ASIC_INTERNAL_SS_ON_HDMI:
  729. percentage = le16_to_cpu(igp_info->info_7.usHDMISSPercentage);
  730. rate = le16_to_cpu(igp_info->info_7.usHDMISSpreadRateIn10Hz);
  731. break;
  732. case ASIC_INTERNAL_SS_ON_LVDS:
  733. percentage = le16_to_cpu(igp_info->info_7.usLvdsSSPercentage);
  734. rate = le16_to_cpu(igp_info->info_7.usLvdsSSpreadRateIn10Hz);
  735. break;
  736. }
  737. break;
  738. case 8:
  739. switch (id) {
  740. case ASIC_INTERNAL_SS_ON_TMDS:
  741. percentage = le16_to_cpu(igp_info->info_8.usDVISSPercentage);
  742. rate = le16_to_cpu(igp_info->info_8.usDVISSpreadRateIn10Hz);
  743. break;
  744. case ASIC_INTERNAL_SS_ON_HDMI:
  745. percentage = le16_to_cpu(igp_info->info_8.usHDMISSPercentage);
  746. rate = le16_to_cpu(igp_info->info_8.usHDMISSpreadRateIn10Hz);
  747. break;
  748. case ASIC_INTERNAL_SS_ON_LVDS:
  749. percentage = le16_to_cpu(igp_info->info_8.usLvdsSSPercentage);
  750. rate = le16_to_cpu(igp_info->info_8.usLvdsSSpreadRateIn10Hz);
  751. break;
  752. }
  753. break;
  754. case 9:
  755. switch (id) {
  756. case ASIC_INTERNAL_SS_ON_TMDS:
  757. percentage = le16_to_cpu(igp_info->info_9.usDVISSPercentage);
  758. rate = le16_to_cpu(igp_info->info_9.usDVISSpreadRateIn10Hz);
  759. break;
  760. case ASIC_INTERNAL_SS_ON_HDMI:
  761. percentage = le16_to_cpu(igp_info->info_9.usHDMISSPercentage);
  762. rate = le16_to_cpu(igp_info->info_9.usHDMISSpreadRateIn10Hz);
  763. break;
  764. case ASIC_INTERNAL_SS_ON_LVDS:
  765. percentage = le16_to_cpu(igp_info->info_9.usLvdsSSPercentage);
  766. rate = le16_to_cpu(igp_info->info_9.usLvdsSSpreadRateIn10Hz);
  767. break;
  768. }
  769. break;
  770. default:
  771. DRM_ERROR("Unsupported IGP table: %d %d\n", frev, crev);
  772. break;
  773. }
  774. if (percentage)
  775. ss->percentage = percentage;
  776. if (rate)
  777. ss->rate = rate;
  778. }
  779. }
  780. union asic_ss_info {
  781. struct _ATOM_ASIC_INTERNAL_SS_INFO info;
  782. struct _ATOM_ASIC_INTERNAL_SS_INFO_V2 info_2;
  783. struct _ATOM_ASIC_INTERNAL_SS_INFO_V3 info_3;
  784. };
  785. union asic_ss_assignment {
  786. struct _ATOM_ASIC_SS_ASSIGNMENT v1;
  787. struct _ATOM_ASIC_SS_ASSIGNMENT_V2 v2;
  788. struct _ATOM_ASIC_SS_ASSIGNMENT_V3 v3;
  789. };
  790. bool amdgpu_atombios_get_asic_ss_info(struct amdgpu_device *adev,
  791. struct amdgpu_atom_ss *ss,
  792. int id, u32 clock)
  793. {
  794. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  795. int index = GetIndexIntoMasterTable(DATA, ASIC_InternalSS_Info);
  796. uint16_t data_offset, size;
  797. union asic_ss_info *ss_info;
  798. union asic_ss_assignment *ss_assign;
  799. uint8_t frev, crev;
  800. int i, num_indices;
  801. if (id == ASIC_INTERNAL_MEMORY_SS) {
  802. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_MEMORY_CLOCK_SS_SUPPORT))
  803. return false;
  804. }
  805. if (id == ASIC_INTERNAL_ENGINE_SS) {
  806. if (!(adev->mode_info.firmware_flags & ATOM_BIOS_INFO_ENGINE_CLOCK_SS_SUPPORT))
  807. return false;
  808. }
  809. memset(ss, 0, sizeof(struct amdgpu_atom_ss));
  810. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, &size,
  811. &frev, &crev, &data_offset)) {
  812. ss_info =
  813. (union asic_ss_info *)(mode_info->atom_context->bios + data_offset);
  814. switch (frev) {
  815. case 1:
  816. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  817. sizeof(ATOM_ASIC_SS_ASSIGNMENT);
  818. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info.asSpreadSpectrum[0]);
  819. for (i = 0; i < num_indices; i++) {
  820. if ((ss_assign->v1.ucClockIndication == id) &&
  821. (clock <= le32_to_cpu(ss_assign->v1.ulTargetClockRange))) {
  822. ss->percentage =
  823. le16_to_cpu(ss_assign->v1.usSpreadSpectrumPercentage);
  824. ss->type = ss_assign->v1.ucSpreadSpectrumMode;
  825. ss->rate = le16_to_cpu(ss_assign->v1.usSpreadRateInKhz);
  826. ss->percentage_divider = 100;
  827. return true;
  828. }
  829. ss_assign = (union asic_ss_assignment *)
  830. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT));
  831. }
  832. break;
  833. case 2:
  834. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  835. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2);
  836. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_2.asSpreadSpectrum[0]);
  837. for (i = 0; i < num_indices; i++) {
  838. if ((ss_assign->v2.ucClockIndication == id) &&
  839. (clock <= le32_to_cpu(ss_assign->v2.ulTargetClockRange))) {
  840. ss->percentage =
  841. le16_to_cpu(ss_assign->v2.usSpreadSpectrumPercentage);
  842. ss->type = ss_assign->v2.ucSpreadSpectrumMode;
  843. ss->rate = le16_to_cpu(ss_assign->v2.usSpreadRateIn10Hz);
  844. ss->percentage_divider = 100;
  845. if ((crev == 2) &&
  846. ((id == ASIC_INTERNAL_ENGINE_SS) ||
  847. (id == ASIC_INTERNAL_MEMORY_SS)))
  848. ss->rate /= 100;
  849. return true;
  850. }
  851. ss_assign = (union asic_ss_assignment *)
  852. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V2));
  853. }
  854. break;
  855. case 3:
  856. num_indices = (size - sizeof(ATOM_COMMON_TABLE_HEADER)) /
  857. sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3);
  858. ss_assign = (union asic_ss_assignment *)((u8 *)&ss_info->info_3.asSpreadSpectrum[0]);
  859. for (i = 0; i < num_indices; i++) {
  860. if ((ss_assign->v3.ucClockIndication == id) &&
  861. (clock <= le32_to_cpu(ss_assign->v3.ulTargetClockRange))) {
  862. ss->percentage =
  863. le16_to_cpu(ss_assign->v3.usSpreadSpectrumPercentage);
  864. ss->type = ss_assign->v3.ucSpreadSpectrumMode;
  865. ss->rate = le16_to_cpu(ss_assign->v3.usSpreadRateIn10Hz);
  866. if (ss_assign->v3.ucSpreadSpectrumMode &
  867. SS_MODE_V3_PERCENTAGE_DIV_BY_1000_MASK)
  868. ss->percentage_divider = 1000;
  869. else
  870. ss->percentage_divider = 100;
  871. if ((id == ASIC_INTERNAL_ENGINE_SS) ||
  872. (id == ASIC_INTERNAL_MEMORY_SS))
  873. ss->rate /= 100;
  874. if (adev->flags & AMD_IS_APU)
  875. amdgpu_atombios_get_igp_ss_overrides(adev, ss, id);
  876. return true;
  877. }
  878. ss_assign = (union asic_ss_assignment *)
  879. ((u8 *)ss_assign + sizeof(ATOM_ASIC_SS_ASSIGNMENT_V3));
  880. }
  881. break;
  882. default:
  883. DRM_ERROR("Unsupported ASIC_InternalSS_Info table: %d %d\n", frev, crev);
  884. break;
  885. }
  886. }
  887. return false;
  888. }
  889. union get_clock_dividers {
  890. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS v1;
  891. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V2 v2;
  892. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V3 v3;
  893. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V4 v4;
  894. struct _COMPUTE_MEMORY_ENGINE_PLL_PARAMETERS_V5 v5;
  895. struct _COMPUTE_GPU_CLOCK_INPUT_PARAMETERS_V1_6 v6_in;
  896. struct _COMPUTE_GPU_CLOCK_OUTPUT_PARAMETERS_V1_6 v6_out;
  897. };
  898. int amdgpu_atombios_get_clock_dividers(struct amdgpu_device *adev,
  899. u8 clock_type,
  900. u32 clock,
  901. bool strobe_mode,
  902. struct atom_clock_dividers *dividers)
  903. {
  904. union get_clock_dividers args;
  905. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryEnginePLL);
  906. u8 frev, crev;
  907. memset(&args, 0, sizeof(args));
  908. memset(dividers, 0, sizeof(struct atom_clock_dividers));
  909. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  910. return -EINVAL;
  911. switch (crev) {
  912. case 2:
  913. case 3:
  914. case 5:
  915. /* r6xx, r7xx, evergreen, ni, si.
  916. * TODO: add support for asic_type <= CHIP_RV770*/
  917. if (clock_type == COMPUTE_ENGINE_PLL_PARAM) {
  918. args.v3.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
  919. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  920. dividers->post_div = args.v3.ucPostDiv;
  921. dividers->enable_post_div = (args.v3.ucCntlFlag &
  922. ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
  923. dividers->enable_dithen = (args.v3.ucCntlFlag &
  924. ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
  925. dividers->whole_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDiv);
  926. dividers->frac_fb_div = le16_to_cpu(args.v3.ulFbDiv.usFbDivFrac);
  927. dividers->ref_div = args.v3.ucRefDiv;
  928. dividers->vco_mode = (args.v3.ucCntlFlag &
  929. ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
  930. } else {
  931. /* for SI we use ComputeMemoryClockParam for memory plls */
  932. if (adev->asic_type >= CHIP_TAHITI)
  933. return -EINVAL;
  934. args.v5.ulClockParams = cpu_to_le32((clock_type << 24) | clock);
  935. if (strobe_mode)
  936. args.v5.ucInputFlag = ATOM_PLL_INPUT_FLAG_PLL_STROBE_MODE_EN;
  937. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  938. dividers->post_div = args.v5.ucPostDiv;
  939. dividers->enable_post_div = (args.v5.ucCntlFlag &
  940. ATOM_PLL_CNTL_FLAG_PLL_POST_DIV_EN) ? true : false;
  941. dividers->enable_dithen = (args.v5.ucCntlFlag &
  942. ATOM_PLL_CNTL_FLAG_FRACTION_DISABLE) ? false : true;
  943. dividers->whole_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDiv);
  944. dividers->frac_fb_div = le16_to_cpu(args.v5.ulFbDiv.usFbDivFrac);
  945. dividers->ref_div = args.v5.ucRefDiv;
  946. dividers->vco_mode = (args.v5.ucCntlFlag &
  947. ATOM_PLL_CNTL_FLAG_MPLL_VCO_MODE) ? 1 : 0;
  948. }
  949. break;
  950. case 4:
  951. /* fusion */
  952. args.v4.ulClock = cpu_to_le32(clock); /* 10 khz */
  953. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  954. dividers->post_divider = dividers->post_div = args.v4.ucPostDiv;
  955. dividers->real_clock = le32_to_cpu(args.v4.ulClock);
  956. break;
  957. case 6:
  958. /* CI */
  959. /* COMPUTE_GPUCLK_INPUT_FLAG_DEFAULT_GPUCLK, COMPUTE_GPUCLK_INPUT_FLAG_SCLK */
  960. args.v6_in.ulClock.ulComputeClockFlag = clock_type;
  961. args.v6_in.ulClock.ulClockFreq = cpu_to_le32(clock); /* 10 khz */
  962. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  963. dividers->whole_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDiv);
  964. dividers->frac_fb_div = le16_to_cpu(args.v6_out.ulFbDiv.usFbDivFrac);
  965. dividers->ref_div = args.v6_out.ucPllRefDiv;
  966. dividers->post_div = args.v6_out.ucPllPostDiv;
  967. dividers->flags = args.v6_out.ucPllCntlFlag;
  968. dividers->real_clock = le32_to_cpu(args.v6_out.ulClock.ulClock);
  969. dividers->post_divider = args.v6_out.ulClock.ucPostDiv;
  970. break;
  971. default:
  972. return -EINVAL;
  973. }
  974. return 0;
  975. }
  976. int amdgpu_atombios_get_memory_pll_dividers(struct amdgpu_device *adev,
  977. u32 clock,
  978. bool strobe_mode,
  979. struct atom_mpll_param *mpll_param)
  980. {
  981. COMPUTE_MEMORY_CLOCK_PARAM_PARAMETERS_V2_1 args;
  982. int index = GetIndexIntoMasterTable(COMMAND, ComputeMemoryClockParam);
  983. u8 frev, crev;
  984. memset(&args, 0, sizeof(args));
  985. memset(mpll_param, 0, sizeof(struct atom_mpll_param));
  986. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  987. return -EINVAL;
  988. switch (frev) {
  989. case 2:
  990. switch (crev) {
  991. case 1:
  992. /* SI */
  993. args.ulClock = cpu_to_le32(clock); /* 10 khz */
  994. args.ucInputFlag = 0;
  995. if (strobe_mode)
  996. args.ucInputFlag |= MPLL_INPUT_FLAG_STROBE_MODE_EN;
  997. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  998. mpll_param->clkfrac = le16_to_cpu(args.ulFbDiv.usFbDivFrac);
  999. mpll_param->clkf = le16_to_cpu(args.ulFbDiv.usFbDiv);
  1000. mpll_param->post_div = args.ucPostDiv;
  1001. mpll_param->dll_speed = args.ucDllSpeed;
  1002. mpll_param->bwcntl = args.ucBWCntl;
  1003. mpll_param->vco_mode =
  1004. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_VCO_MODE_MASK);
  1005. mpll_param->yclk_sel =
  1006. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_BYPASS_DQ_PLL) ? 1 : 0;
  1007. mpll_param->qdr =
  1008. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_QDR_ENABLE) ? 1 : 0;
  1009. mpll_param->half_rate =
  1010. (args.ucPllCntlFlag & MPLL_CNTL_FLAG_AD_HALF_RATE) ? 1 : 0;
  1011. break;
  1012. default:
  1013. return -EINVAL;
  1014. }
  1015. break;
  1016. default:
  1017. return -EINVAL;
  1018. }
  1019. return 0;
  1020. }
  1021. void amdgpu_atombios_set_engine_dram_timings(struct amdgpu_device *adev,
  1022. u32 eng_clock, u32 mem_clock)
  1023. {
  1024. SET_ENGINE_CLOCK_PS_ALLOCATION args;
  1025. int index = GetIndexIntoMasterTable(COMMAND, DynamicMemorySettings);
  1026. u32 tmp;
  1027. memset(&args, 0, sizeof(args));
  1028. tmp = eng_clock & SET_CLOCK_FREQ_MASK;
  1029. tmp |= (COMPUTE_ENGINE_PLL_PARAM << 24);
  1030. args.ulTargetEngineClock = cpu_to_le32(tmp);
  1031. if (mem_clock)
  1032. args.sReserved.ulClock = cpu_to_le32(mem_clock & SET_CLOCK_FREQ_MASK);
  1033. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1034. }
  1035. void amdgpu_atombios_get_default_voltages(struct amdgpu_device *adev,
  1036. u16 *vddc, u16 *vddci, u16 *mvdd)
  1037. {
  1038. struct amdgpu_mode_info *mode_info = &adev->mode_info;
  1039. int index = GetIndexIntoMasterTable(DATA, FirmwareInfo);
  1040. u8 frev, crev;
  1041. u16 data_offset;
  1042. union firmware_info *firmware_info;
  1043. *vddc = 0;
  1044. *vddci = 0;
  1045. *mvdd = 0;
  1046. if (amdgpu_atom_parse_data_header(mode_info->atom_context, index, NULL,
  1047. &frev, &crev, &data_offset)) {
  1048. firmware_info =
  1049. (union firmware_info *)(mode_info->atom_context->bios +
  1050. data_offset);
  1051. *vddc = le16_to_cpu(firmware_info->info_14.usBootUpVDDCVoltage);
  1052. if ((frev == 2) && (crev >= 2)) {
  1053. *vddci = le16_to_cpu(firmware_info->info_22.usBootUpVDDCIVoltage);
  1054. *mvdd = le16_to_cpu(firmware_info->info_22.usBootUpMVDDCVoltage);
  1055. }
  1056. }
  1057. }
  1058. union set_voltage {
  1059. struct _SET_VOLTAGE_PS_ALLOCATION alloc;
  1060. struct _SET_VOLTAGE_PARAMETERS v1;
  1061. struct _SET_VOLTAGE_PARAMETERS_V2 v2;
  1062. struct _SET_VOLTAGE_PARAMETERS_V1_3 v3;
  1063. };
  1064. int amdgpu_atombios_get_max_vddc(struct amdgpu_device *adev, u8 voltage_type,
  1065. u16 voltage_id, u16 *voltage)
  1066. {
  1067. union set_voltage args;
  1068. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1069. u8 frev, crev;
  1070. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1071. return -EINVAL;
  1072. switch (crev) {
  1073. case 1:
  1074. return -EINVAL;
  1075. case 2:
  1076. args.v2.ucVoltageType = SET_VOLTAGE_GET_MAX_VOLTAGE;
  1077. args.v2.ucVoltageMode = 0;
  1078. args.v2.usVoltageLevel = 0;
  1079. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1080. *voltage = le16_to_cpu(args.v2.usVoltageLevel);
  1081. break;
  1082. case 3:
  1083. args.v3.ucVoltageType = voltage_type;
  1084. args.v3.ucVoltageMode = ATOM_GET_VOLTAGE_LEVEL;
  1085. args.v3.usVoltageLevel = cpu_to_le16(voltage_id);
  1086. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1087. *voltage = le16_to_cpu(args.v3.usVoltageLevel);
  1088. break;
  1089. default:
  1090. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1091. return -EINVAL;
  1092. }
  1093. return 0;
  1094. }
  1095. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_idx(struct amdgpu_device *adev,
  1096. u16 *voltage,
  1097. u16 leakage_idx)
  1098. {
  1099. return amdgpu_atombios_get_max_vddc(adev, VOLTAGE_TYPE_VDDC, leakage_idx, voltage);
  1100. }
  1101. int amdgpu_atombios_get_leakage_id_from_vbios(struct amdgpu_device *adev,
  1102. u16 *leakage_id)
  1103. {
  1104. union set_voltage args;
  1105. int index = GetIndexIntoMasterTable(COMMAND, SetVoltage);
  1106. u8 frev, crev;
  1107. if (!amdgpu_atom_parse_cmd_header(adev->mode_info.atom_context, index, &frev, &crev))
  1108. return -EINVAL;
  1109. switch (crev) {
  1110. case 3:
  1111. case 4:
  1112. args.v3.ucVoltageType = 0;
  1113. args.v3.ucVoltageMode = ATOM_GET_LEAKAGE_ID;
  1114. args.v3.usVoltageLevel = 0;
  1115. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1116. *leakage_id = le16_to_cpu(args.v3.usVoltageLevel);
  1117. break;
  1118. default:
  1119. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1120. return -EINVAL;
  1121. }
  1122. return 0;
  1123. }
  1124. int amdgpu_atombios_get_leakage_vddc_based_on_leakage_params(struct amdgpu_device *adev,
  1125. u16 *vddc, u16 *vddci,
  1126. u16 virtual_voltage_id,
  1127. u16 vbios_voltage_id)
  1128. {
  1129. int index = GetIndexIntoMasterTable(DATA, ASIC_ProfilingInfo);
  1130. u8 frev, crev;
  1131. u16 data_offset, size;
  1132. int i, j;
  1133. ATOM_ASIC_PROFILING_INFO_V2_1 *profile;
  1134. u16 *leakage_bin, *vddc_id_buf, *vddc_buf, *vddci_id_buf, *vddci_buf;
  1135. *vddc = 0;
  1136. *vddci = 0;
  1137. if (!amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1138. &frev, &crev, &data_offset))
  1139. return -EINVAL;
  1140. profile = (ATOM_ASIC_PROFILING_INFO_V2_1 *)
  1141. (adev->mode_info.atom_context->bios + data_offset);
  1142. switch (frev) {
  1143. case 1:
  1144. return -EINVAL;
  1145. case 2:
  1146. switch (crev) {
  1147. case 1:
  1148. if (size < sizeof(ATOM_ASIC_PROFILING_INFO_V2_1))
  1149. return -EINVAL;
  1150. leakage_bin = (u16 *)
  1151. (adev->mode_info.atom_context->bios + data_offset +
  1152. le16_to_cpu(profile->usLeakageBinArrayOffset));
  1153. vddc_id_buf = (u16 *)
  1154. (adev->mode_info.atom_context->bios + data_offset +
  1155. le16_to_cpu(profile->usElbVDDC_IdArrayOffset));
  1156. vddc_buf = (u16 *)
  1157. (adev->mode_info.atom_context->bios + data_offset +
  1158. le16_to_cpu(profile->usElbVDDC_LevelArrayOffset));
  1159. vddci_id_buf = (u16 *)
  1160. (adev->mode_info.atom_context->bios + data_offset +
  1161. le16_to_cpu(profile->usElbVDDCI_IdArrayOffset));
  1162. vddci_buf = (u16 *)
  1163. (adev->mode_info.atom_context->bios + data_offset +
  1164. le16_to_cpu(profile->usElbVDDCI_LevelArrayOffset));
  1165. if (profile->ucElbVDDC_Num > 0) {
  1166. for (i = 0; i < profile->ucElbVDDC_Num; i++) {
  1167. if (vddc_id_buf[i] == virtual_voltage_id) {
  1168. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1169. if (vbios_voltage_id <= leakage_bin[j]) {
  1170. *vddc = vddc_buf[j * profile->ucElbVDDC_Num + i];
  1171. break;
  1172. }
  1173. }
  1174. break;
  1175. }
  1176. }
  1177. }
  1178. if (profile->ucElbVDDCI_Num > 0) {
  1179. for (i = 0; i < profile->ucElbVDDCI_Num; i++) {
  1180. if (vddci_id_buf[i] == virtual_voltage_id) {
  1181. for (j = 0; j < profile->ucLeakageBinNum; j++) {
  1182. if (vbios_voltage_id <= leakage_bin[j]) {
  1183. *vddci = vddci_buf[j * profile->ucElbVDDCI_Num + i];
  1184. break;
  1185. }
  1186. }
  1187. break;
  1188. }
  1189. }
  1190. }
  1191. break;
  1192. default:
  1193. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1194. return -EINVAL;
  1195. }
  1196. break;
  1197. default:
  1198. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1199. return -EINVAL;
  1200. }
  1201. return 0;
  1202. }
  1203. union get_voltage_info {
  1204. struct _GET_VOLTAGE_INFO_INPUT_PARAMETER_V1_2 in;
  1205. struct _GET_EVV_VOLTAGE_INFO_OUTPUT_PARAMETER_V1_2 evv_out;
  1206. };
  1207. int amdgpu_atombios_get_voltage_evv(struct amdgpu_device *adev,
  1208. u16 virtual_voltage_id,
  1209. u16 *voltage)
  1210. {
  1211. int index = GetIndexIntoMasterTable(COMMAND, GetVoltageInfo);
  1212. u32 entry_id;
  1213. u32 count = adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.count;
  1214. union get_voltage_info args;
  1215. for (entry_id = 0; entry_id < count; entry_id++) {
  1216. if (adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].v ==
  1217. virtual_voltage_id)
  1218. break;
  1219. }
  1220. if (entry_id >= count)
  1221. return -EINVAL;
  1222. args.in.ucVoltageType = VOLTAGE_TYPE_VDDC;
  1223. args.in.ucVoltageMode = ATOM_GET_VOLTAGE_EVV_VOLTAGE;
  1224. args.in.usVoltageLevel = cpu_to_le16(virtual_voltage_id);
  1225. args.in.ulSCLKFreq =
  1226. cpu_to_le32(adev->pm.dpm.dyn_state.vddc_dependency_on_sclk.entries[entry_id].clk);
  1227. amdgpu_atom_execute_table(adev->mode_info.atom_context, index, (uint32_t *)&args);
  1228. *voltage = le16_to_cpu(args.evv_out.usVoltageLevel);
  1229. return 0;
  1230. }
  1231. union voltage_object_info {
  1232. struct _ATOM_VOLTAGE_OBJECT_INFO v1;
  1233. struct _ATOM_VOLTAGE_OBJECT_INFO_V2 v2;
  1234. struct _ATOM_VOLTAGE_OBJECT_INFO_V3_1 v3;
  1235. };
  1236. union voltage_object {
  1237. struct _ATOM_VOLTAGE_OBJECT v1;
  1238. struct _ATOM_VOLTAGE_OBJECT_V2 v2;
  1239. union _ATOM_VOLTAGE_OBJECT_V3 v3;
  1240. };
  1241. static ATOM_VOLTAGE_OBJECT_V3 *amdgpu_atombios_lookup_voltage_object_v3(ATOM_VOLTAGE_OBJECT_INFO_V3_1 *v3,
  1242. u8 voltage_type, u8 voltage_mode)
  1243. {
  1244. u32 size = le16_to_cpu(v3->sHeader.usStructureSize);
  1245. u32 offset = offsetof(ATOM_VOLTAGE_OBJECT_INFO_V3_1, asVoltageObj[0]);
  1246. u8 *start = (u8*)v3;
  1247. while (offset < size) {
  1248. ATOM_VOLTAGE_OBJECT_V3 *vo = (ATOM_VOLTAGE_OBJECT_V3 *)(start + offset);
  1249. if ((vo->asGpioVoltageObj.sHeader.ucVoltageType == voltage_type) &&
  1250. (vo->asGpioVoltageObj.sHeader.ucVoltageMode == voltage_mode))
  1251. return vo;
  1252. offset += le16_to_cpu(vo->asGpioVoltageObj.sHeader.usSize);
  1253. }
  1254. return NULL;
  1255. }
  1256. int amdgpu_atombios_get_svi2_info(struct amdgpu_device *adev,
  1257. u8 voltage_type,
  1258. u8 *svd_gpio_id, u8 *svc_gpio_id)
  1259. {
  1260. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1261. u8 frev, crev;
  1262. u16 data_offset, size;
  1263. union voltage_object_info *voltage_info;
  1264. union voltage_object *voltage_object = NULL;
  1265. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1266. &frev, &crev, &data_offset)) {
  1267. voltage_info = (union voltage_object_info *)
  1268. (adev->mode_info.atom_context->bios + data_offset);
  1269. switch (frev) {
  1270. case 3:
  1271. switch (crev) {
  1272. case 1:
  1273. voltage_object = (union voltage_object *)
  1274. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1275. voltage_type,
  1276. VOLTAGE_OBJ_SVID2);
  1277. if (voltage_object) {
  1278. *svd_gpio_id = voltage_object->v3.asSVID2Obj.ucSVDGpioId;
  1279. *svc_gpio_id = voltage_object->v3.asSVID2Obj.ucSVCGpioId;
  1280. } else {
  1281. return -EINVAL;
  1282. }
  1283. break;
  1284. default:
  1285. DRM_ERROR("unknown voltage object table\n");
  1286. return -EINVAL;
  1287. }
  1288. break;
  1289. default:
  1290. DRM_ERROR("unknown voltage object table\n");
  1291. return -EINVAL;
  1292. }
  1293. }
  1294. return 0;
  1295. }
  1296. bool
  1297. amdgpu_atombios_is_voltage_gpio(struct amdgpu_device *adev,
  1298. u8 voltage_type, u8 voltage_mode)
  1299. {
  1300. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1301. u8 frev, crev;
  1302. u16 data_offset, size;
  1303. union voltage_object_info *voltage_info;
  1304. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1305. &frev, &crev, &data_offset)) {
  1306. voltage_info = (union voltage_object_info *)
  1307. (adev->mode_info.atom_context->bios + data_offset);
  1308. switch (frev) {
  1309. case 3:
  1310. switch (crev) {
  1311. case 1:
  1312. if (amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1313. voltage_type, voltage_mode))
  1314. return true;
  1315. break;
  1316. default:
  1317. DRM_ERROR("unknown voltage object table\n");
  1318. return false;
  1319. }
  1320. break;
  1321. default:
  1322. DRM_ERROR("unknown voltage object table\n");
  1323. return false;
  1324. }
  1325. }
  1326. return false;
  1327. }
  1328. int amdgpu_atombios_get_voltage_table(struct amdgpu_device *adev,
  1329. u8 voltage_type, u8 voltage_mode,
  1330. struct atom_voltage_table *voltage_table)
  1331. {
  1332. int index = GetIndexIntoMasterTable(DATA, VoltageObjectInfo);
  1333. u8 frev, crev;
  1334. u16 data_offset, size;
  1335. int i;
  1336. union voltage_object_info *voltage_info;
  1337. union voltage_object *voltage_object = NULL;
  1338. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1339. &frev, &crev, &data_offset)) {
  1340. voltage_info = (union voltage_object_info *)
  1341. (adev->mode_info.atom_context->bios + data_offset);
  1342. switch (frev) {
  1343. case 3:
  1344. switch (crev) {
  1345. case 1:
  1346. voltage_object = (union voltage_object *)
  1347. amdgpu_atombios_lookup_voltage_object_v3(&voltage_info->v3,
  1348. voltage_type, voltage_mode);
  1349. if (voltage_object) {
  1350. ATOM_GPIO_VOLTAGE_OBJECT_V3 *gpio =
  1351. &voltage_object->v3.asGpioVoltageObj;
  1352. VOLTAGE_LUT_ENTRY_V2 *lut;
  1353. if (gpio->ucGpioEntryNum > MAX_VOLTAGE_ENTRIES)
  1354. return -EINVAL;
  1355. lut = &gpio->asVolGpioLut[0];
  1356. for (i = 0; i < gpio->ucGpioEntryNum; i++) {
  1357. voltage_table->entries[i].value =
  1358. le16_to_cpu(lut->usVoltageValue);
  1359. voltage_table->entries[i].smio_low =
  1360. le32_to_cpu(lut->ulVoltageId);
  1361. lut = (VOLTAGE_LUT_ENTRY_V2 *)
  1362. ((u8 *)lut + sizeof(VOLTAGE_LUT_ENTRY_V2));
  1363. }
  1364. voltage_table->mask_low = le32_to_cpu(gpio->ulGpioMaskVal);
  1365. voltage_table->count = gpio->ucGpioEntryNum;
  1366. voltage_table->phase_delay = gpio->ucPhaseDelay;
  1367. return 0;
  1368. }
  1369. break;
  1370. default:
  1371. DRM_ERROR("unknown voltage object table\n");
  1372. return -EINVAL;
  1373. }
  1374. break;
  1375. default:
  1376. DRM_ERROR("unknown voltage object table\n");
  1377. return -EINVAL;
  1378. }
  1379. }
  1380. return -EINVAL;
  1381. }
  1382. union vram_info {
  1383. struct _ATOM_VRAM_INFO_V3 v1_3;
  1384. struct _ATOM_VRAM_INFO_V4 v1_4;
  1385. struct _ATOM_VRAM_INFO_HEADER_V2_1 v2_1;
  1386. };
  1387. #define MEM_ID_MASK 0xff000000
  1388. #define MEM_ID_SHIFT 24
  1389. #define CLOCK_RANGE_MASK 0x00ffffff
  1390. #define CLOCK_RANGE_SHIFT 0
  1391. #define LOW_NIBBLE_MASK 0xf
  1392. #define DATA_EQU_PREV 0
  1393. #define DATA_FROM_TABLE 4
  1394. int amdgpu_atombios_init_mc_reg_table(struct amdgpu_device *adev,
  1395. u8 module_index,
  1396. struct atom_mc_reg_table *reg_table)
  1397. {
  1398. int index = GetIndexIntoMasterTable(DATA, VRAM_Info);
  1399. u8 frev, crev, num_entries, t_mem_id, num_ranges = 0;
  1400. u32 i = 0, j;
  1401. u16 data_offset, size;
  1402. union vram_info *vram_info;
  1403. memset(reg_table, 0, sizeof(struct atom_mc_reg_table));
  1404. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1405. &frev, &crev, &data_offset)) {
  1406. vram_info = (union vram_info *)
  1407. (adev->mode_info.atom_context->bios + data_offset);
  1408. switch (frev) {
  1409. case 1:
  1410. DRM_ERROR("old table version %d, %d\n", frev, crev);
  1411. return -EINVAL;
  1412. case 2:
  1413. switch (crev) {
  1414. case 1:
  1415. if (module_index < vram_info->v2_1.ucNumOfVRAMModule) {
  1416. ATOM_INIT_REG_BLOCK *reg_block =
  1417. (ATOM_INIT_REG_BLOCK *)
  1418. ((u8 *)vram_info + le16_to_cpu(vram_info->v2_1.usMemClkPatchTblOffset));
  1419. ATOM_MEMORY_SETTING_DATA_BLOCK *reg_data =
  1420. (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1421. ((u8 *)reg_block + (2 * sizeof(u16)) +
  1422. le16_to_cpu(reg_block->usRegIndexTblSize));
  1423. ATOM_INIT_REG_INDEX_FORMAT *format = &reg_block->asRegIndexBuf[0];
  1424. num_entries = (u8)((le16_to_cpu(reg_block->usRegIndexTblSize)) /
  1425. sizeof(ATOM_INIT_REG_INDEX_FORMAT)) - 1;
  1426. if (num_entries > VBIOS_MC_REGISTER_ARRAY_SIZE)
  1427. return -EINVAL;
  1428. while (i < num_entries) {
  1429. if (format->ucPreRegDataLength & ACCESS_PLACEHOLDER)
  1430. break;
  1431. reg_table->mc_reg_address[i].s1 =
  1432. (u16)(le16_to_cpu(format->usRegIndex));
  1433. reg_table->mc_reg_address[i].pre_reg_data =
  1434. (u8)(format->ucPreRegDataLength);
  1435. i++;
  1436. format = (ATOM_INIT_REG_INDEX_FORMAT *)
  1437. ((u8 *)format + sizeof(ATOM_INIT_REG_INDEX_FORMAT));
  1438. }
  1439. reg_table->last = i;
  1440. while ((le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK) &&
  1441. (num_ranges < VBIOS_MAX_AC_TIMING_ENTRIES)) {
  1442. t_mem_id = (u8)((le32_to_cpu(*(u32 *)reg_data) & MEM_ID_MASK)
  1443. >> MEM_ID_SHIFT);
  1444. if (module_index == t_mem_id) {
  1445. reg_table->mc_reg_table_entry[num_ranges].mclk_max =
  1446. (u32)((le32_to_cpu(*(u32 *)reg_data) & CLOCK_RANGE_MASK)
  1447. >> CLOCK_RANGE_SHIFT);
  1448. for (i = 0, j = 1; i < reg_table->last; i++) {
  1449. if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_FROM_TABLE) {
  1450. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1451. (u32)le32_to_cpu(*((u32 *)reg_data + j));
  1452. j++;
  1453. } else if ((reg_table->mc_reg_address[i].pre_reg_data & LOW_NIBBLE_MASK) == DATA_EQU_PREV) {
  1454. reg_table->mc_reg_table_entry[num_ranges].mc_data[i] =
  1455. reg_table->mc_reg_table_entry[num_ranges].mc_data[i - 1];
  1456. }
  1457. }
  1458. num_ranges++;
  1459. }
  1460. reg_data = (ATOM_MEMORY_SETTING_DATA_BLOCK *)
  1461. ((u8 *)reg_data + le16_to_cpu(reg_block->usRegDataBlkSize));
  1462. }
  1463. if (le32_to_cpu(*(u32 *)reg_data) != END_OF_REG_DATA_BLOCK)
  1464. return -EINVAL;
  1465. reg_table->num_entries = num_ranges;
  1466. } else
  1467. return -EINVAL;
  1468. break;
  1469. default:
  1470. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1471. return -EINVAL;
  1472. }
  1473. break;
  1474. default:
  1475. DRM_ERROR("Unknown table version %d, %d\n", frev, crev);
  1476. return -EINVAL;
  1477. }
  1478. return 0;
  1479. }
  1480. return -EINVAL;
  1481. }
  1482. bool amdgpu_atombios_has_gpu_virtualization_table(struct amdgpu_device *adev)
  1483. {
  1484. int index = GetIndexIntoMasterTable(DATA, GPUVirtualizationInfo);
  1485. u8 frev, crev;
  1486. u16 data_offset, size;
  1487. if (amdgpu_atom_parse_data_header(adev->mode_info.atom_context, index, &size,
  1488. &frev, &crev, &data_offset))
  1489. return true;
  1490. return false;
  1491. }
  1492. void amdgpu_atombios_scratch_regs_lock(struct amdgpu_device *adev, bool lock)
  1493. {
  1494. uint32_t bios_6_scratch;
  1495. bios_6_scratch = RREG32(adev->bios_scratch_reg_offset + 6);
  1496. if (lock) {
  1497. bios_6_scratch |= ATOM_S6_CRITICAL_STATE;
  1498. bios_6_scratch &= ~ATOM_S6_ACC_MODE;
  1499. } else {
  1500. bios_6_scratch &= ~ATOM_S6_CRITICAL_STATE;
  1501. bios_6_scratch |= ATOM_S6_ACC_MODE;
  1502. }
  1503. WREG32(adev->bios_scratch_reg_offset + 6, bios_6_scratch);
  1504. }
  1505. static void amdgpu_atombios_scratch_regs_init(struct amdgpu_device *adev)
  1506. {
  1507. uint32_t bios_2_scratch, bios_6_scratch;
  1508. adev->bios_scratch_reg_offset = mmBIOS_SCRATCH_0;
  1509. bios_2_scratch = RREG32(adev->bios_scratch_reg_offset + 2);
  1510. bios_6_scratch = RREG32(adev->bios_scratch_reg_offset + 6);
  1511. /* let the bios control the backlight */
  1512. bios_2_scratch &= ~ATOM_S2_VRI_BRIGHT_ENABLE;
  1513. /* tell the bios not to handle mode switching */
  1514. bios_6_scratch |= ATOM_S6_ACC_BLOCK_DISPLAY_SWITCH;
  1515. /* clear the vbios dpms state */
  1516. bios_2_scratch &= ~ATOM_S2_DEVICE_DPMS_STATE;
  1517. WREG32(adev->bios_scratch_reg_offset + 2, bios_2_scratch);
  1518. WREG32(adev->bios_scratch_reg_offset + 6, bios_6_scratch);
  1519. }
  1520. void amdgpu_atombios_scratch_regs_engine_hung(struct amdgpu_device *adev,
  1521. bool hung)
  1522. {
  1523. u32 tmp = RREG32(adev->bios_scratch_reg_offset + 3);
  1524. if (hung)
  1525. tmp |= ATOM_S3_ASIC_GUI_ENGINE_HUNG;
  1526. else
  1527. tmp &= ~ATOM_S3_ASIC_GUI_ENGINE_HUNG;
  1528. WREG32(adev->bios_scratch_reg_offset + 3, tmp);
  1529. }
  1530. bool amdgpu_atombios_scratch_need_asic_init(struct amdgpu_device *adev)
  1531. {
  1532. u32 tmp = RREG32(adev->bios_scratch_reg_offset + 7);
  1533. if (tmp & ATOM_S7_ASIC_INIT_COMPLETE_MASK)
  1534. return false;
  1535. else
  1536. return true;
  1537. }
  1538. /* Atom needs data in little endian format so swap as appropriate when copying
  1539. * data to or from atom. Note that atom operates on dw units.
  1540. *
  1541. * Use to_le=true when sending data to atom and provide at least
  1542. * ALIGN(num_bytes,4) bytes in the dst buffer.
  1543. *
  1544. * Use to_le=false when receiving data from atom and provide ALIGN(num_bytes,4)
  1545. * byes in the src buffer.
  1546. */
  1547. void amdgpu_atombios_copy_swap(u8 *dst, u8 *src, u8 num_bytes, bool to_le)
  1548. {
  1549. #ifdef __BIG_ENDIAN
  1550. u32 src_tmp[5], dst_tmp[5];
  1551. int i;
  1552. u8 align_num_bytes = ALIGN(num_bytes, 4);
  1553. if (to_le) {
  1554. memcpy(src_tmp, src, num_bytes);
  1555. for (i = 0; i < align_num_bytes / 4; i++)
  1556. dst_tmp[i] = cpu_to_le32(src_tmp[i]);
  1557. memcpy(dst, dst_tmp, align_num_bytes);
  1558. } else {
  1559. memcpy(src_tmp, src, align_num_bytes);
  1560. for (i = 0; i < align_num_bytes / 4; i++)
  1561. dst_tmp[i] = le32_to_cpu(src_tmp[i]);
  1562. memcpy(dst, dst_tmp, num_bytes);
  1563. }
  1564. #else
  1565. memcpy(dst, src, num_bytes);
  1566. #endif
  1567. }
  1568. static int amdgpu_atombios_allocate_fb_scratch(struct amdgpu_device *adev)
  1569. {
  1570. struct atom_context *ctx = adev->mode_info.atom_context;
  1571. int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
  1572. uint16_t data_offset;
  1573. int usage_bytes = 0;
  1574. struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
  1575. u64 start_addr;
  1576. u64 size;
  1577. if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
  1578. firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
  1579. DRM_DEBUG("atom firmware requested %08x %dkb\n",
  1580. le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
  1581. le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
  1582. start_addr = firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware;
  1583. size = firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb;
  1584. if ((uint32_t)(start_addr & ATOM_VRAM_OPERATION_FLAGS_MASK) ==
  1585. (uint32_t)(ATOM_VRAM_BLOCK_SRIOV_MSG_SHARE_RESERVATION <<
  1586. ATOM_VRAM_OPERATION_FLAGS_SHIFT)) {
  1587. /* Firmware request VRAM reservation for SR-IOV */
  1588. adev->fw_vram_usage.start_offset = (start_addr &
  1589. (~ATOM_VRAM_OPERATION_FLAGS_MASK)) << 10;
  1590. adev->fw_vram_usage.size = size << 10;
  1591. /* Use the default scratch size */
  1592. usage_bytes = 0;
  1593. } else {
  1594. usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
  1595. }
  1596. }
  1597. ctx->scratch_size_bytes = 0;
  1598. if (usage_bytes == 0)
  1599. usage_bytes = 20 * 1024;
  1600. /* allocate some scratch memory */
  1601. ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
  1602. if (!ctx->scratch)
  1603. return -ENOMEM;
  1604. ctx->scratch_size_bytes = usage_bytes;
  1605. return 0;
  1606. }
  1607. /* ATOM accessor methods */
  1608. /*
  1609. * ATOM is an interpreted byte code stored in tables in the vbios. The
  1610. * driver registers callbacks to access registers and the interpreter
  1611. * in the driver parses the tables and executes then to program specific
  1612. * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
  1613. * atombios.h, and atom.c
  1614. */
  1615. /**
  1616. * cail_pll_read - read PLL register
  1617. *
  1618. * @info: atom card_info pointer
  1619. * @reg: PLL register offset
  1620. *
  1621. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  1622. * Returns the value of the PLL register.
  1623. */
  1624. static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
  1625. {
  1626. return 0;
  1627. }
  1628. /**
  1629. * cail_pll_write - write PLL register
  1630. *
  1631. * @info: atom card_info pointer
  1632. * @reg: PLL register offset
  1633. * @val: value to write to the pll register
  1634. *
  1635. * Provides a PLL register accessor for the atom interpreter (r4xx+).
  1636. */
  1637. static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
  1638. {
  1639. }
  1640. /**
  1641. * cail_mc_read - read MC (Memory Controller) register
  1642. *
  1643. * @info: atom card_info pointer
  1644. * @reg: MC register offset
  1645. *
  1646. * Provides an MC register accessor for the atom interpreter (r4xx+).
  1647. * Returns the value of the MC register.
  1648. */
  1649. static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
  1650. {
  1651. return 0;
  1652. }
  1653. /**
  1654. * cail_mc_write - write MC (Memory Controller) register
  1655. *
  1656. * @info: atom card_info pointer
  1657. * @reg: MC register offset
  1658. * @val: value to write to the pll register
  1659. *
  1660. * Provides a MC register accessor for the atom interpreter (r4xx+).
  1661. */
  1662. static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
  1663. {
  1664. }
  1665. /**
  1666. * cail_reg_write - write MMIO register
  1667. *
  1668. * @info: atom card_info pointer
  1669. * @reg: MMIO register offset
  1670. * @val: value to write to the pll register
  1671. *
  1672. * Provides a MMIO register accessor for the atom interpreter (r4xx+).
  1673. */
  1674. static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
  1675. {
  1676. struct amdgpu_device *adev = info->dev->dev_private;
  1677. WREG32(reg, val);
  1678. }
  1679. /**
  1680. * cail_reg_read - read MMIO register
  1681. *
  1682. * @info: atom card_info pointer
  1683. * @reg: MMIO register offset
  1684. *
  1685. * Provides an MMIO register accessor for the atom interpreter (r4xx+).
  1686. * Returns the value of the MMIO register.
  1687. */
  1688. static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
  1689. {
  1690. struct amdgpu_device *adev = info->dev->dev_private;
  1691. uint32_t r;
  1692. r = RREG32(reg);
  1693. return r;
  1694. }
  1695. /**
  1696. * cail_ioreg_write - write IO register
  1697. *
  1698. * @info: atom card_info pointer
  1699. * @reg: IO register offset
  1700. * @val: value to write to the pll register
  1701. *
  1702. * Provides a IO register accessor for the atom interpreter (r4xx+).
  1703. */
  1704. static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
  1705. {
  1706. struct amdgpu_device *adev = info->dev->dev_private;
  1707. WREG32_IO(reg, val);
  1708. }
  1709. /**
  1710. * cail_ioreg_read - read IO register
  1711. *
  1712. * @info: atom card_info pointer
  1713. * @reg: IO register offset
  1714. *
  1715. * Provides an IO register accessor for the atom interpreter (r4xx+).
  1716. * Returns the value of the IO register.
  1717. */
  1718. static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
  1719. {
  1720. struct amdgpu_device *adev = info->dev->dev_private;
  1721. uint32_t r;
  1722. r = RREG32_IO(reg);
  1723. return r;
  1724. }
  1725. static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
  1726. struct device_attribute *attr,
  1727. char *buf)
  1728. {
  1729. struct drm_device *ddev = dev_get_drvdata(dev);
  1730. struct amdgpu_device *adev = ddev->dev_private;
  1731. struct atom_context *ctx = adev->mode_info.atom_context;
  1732. return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
  1733. }
  1734. static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
  1735. NULL);
  1736. /**
  1737. * amdgpu_atombios_fini - free the driver info and callbacks for atombios
  1738. *
  1739. * @adev: amdgpu_device pointer
  1740. *
  1741. * Frees the driver info and register access callbacks for the ATOM
  1742. * interpreter (r4xx+).
  1743. * Called at driver shutdown.
  1744. */
  1745. void amdgpu_atombios_fini(struct amdgpu_device *adev)
  1746. {
  1747. if (adev->mode_info.atom_context) {
  1748. kfree(adev->mode_info.atom_context->scratch);
  1749. kfree(adev->mode_info.atom_context->iio);
  1750. }
  1751. kfree(adev->mode_info.atom_context);
  1752. adev->mode_info.atom_context = NULL;
  1753. kfree(adev->mode_info.atom_card_info);
  1754. adev->mode_info.atom_card_info = NULL;
  1755. device_remove_file(adev->dev, &dev_attr_vbios_version);
  1756. }
  1757. /**
  1758. * amdgpu_atombios_init - init the driver info and callbacks for atombios
  1759. *
  1760. * @adev: amdgpu_device pointer
  1761. *
  1762. * Initializes the driver info and register access callbacks for the
  1763. * ATOM interpreter (r4xx+).
  1764. * Returns 0 on sucess, -ENOMEM on failure.
  1765. * Called at driver startup.
  1766. */
  1767. int amdgpu_atombios_init(struct amdgpu_device *adev)
  1768. {
  1769. struct card_info *atom_card_info =
  1770. kzalloc(sizeof(struct card_info), GFP_KERNEL);
  1771. int ret;
  1772. if (!atom_card_info)
  1773. return -ENOMEM;
  1774. adev->mode_info.atom_card_info = atom_card_info;
  1775. atom_card_info->dev = adev->ddev;
  1776. atom_card_info->reg_read = cail_reg_read;
  1777. atom_card_info->reg_write = cail_reg_write;
  1778. /* needed for iio ops */
  1779. if (adev->rio_mem) {
  1780. atom_card_info->ioreg_read = cail_ioreg_read;
  1781. atom_card_info->ioreg_write = cail_ioreg_write;
  1782. } else {
  1783. DRM_DEBUG("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
  1784. atom_card_info->ioreg_read = cail_reg_read;
  1785. atom_card_info->ioreg_write = cail_reg_write;
  1786. }
  1787. atom_card_info->mc_read = cail_mc_read;
  1788. atom_card_info->mc_write = cail_mc_write;
  1789. atom_card_info->pll_read = cail_pll_read;
  1790. atom_card_info->pll_write = cail_pll_write;
  1791. adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
  1792. if (!adev->mode_info.atom_context) {
  1793. amdgpu_atombios_fini(adev);
  1794. return -ENOMEM;
  1795. }
  1796. mutex_init(&adev->mode_info.atom_context->mutex);
  1797. if (adev->is_atom_fw) {
  1798. amdgpu_atomfirmware_scratch_regs_init(adev);
  1799. amdgpu_atomfirmware_allocate_fb_scratch(adev);
  1800. } else {
  1801. amdgpu_atombios_scratch_regs_init(adev);
  1802. amdgpu_atombios_allocate_fb_scratch(adev);
  1803. }
  1804. ret = device_create_file(adev->dev, &dev_attr_vbios_version);
  1805. if (ret) {
  1806. DRM_ERROR("Failed to create device file for VBIOS version\n");
  1807. return ret;
  1808. }
  1809. return 0;
  1810. }