amdgpu_atombios.c 62 KB

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