base.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * Copyright 2013 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Ben Skeggs
  23. */
  24. #include "priv.h"
  25. #include <subdev/bios.h>
  26. #include <subdev/bios/boost.h>
  27. #include <subdev/bios/cstep.h>
  28. #include <subdev/bios/perf.h>
  29. #include <subdev/bios/vpstate.h>
  30. #include <subdev/fb.h>
  31. #include <subdev/therm.h>
  32. #include <subdev/volt.h>
  33. #include <core/option.h>
  34. /******************************************************************************
  35. * misc
  36. *****************************************************************************/
  37. static u32
  38. nvkm_clk_adjust(struct nvkm_clk *clk, bool adjust,
  39. u8 pstate, u8 domain, u32 input)
  40. {
  41. struct nvkm_bios *bios = clk->subdev.device->bios;
  42. struct nvbios_boostE boostE;
  43. u8 ver, hdr, cnt, len;
  44. u32 data;
  45. data = nvbios_boostEm(bios, pstate, &ver, &hdr, &cnt, &len, &boostE);
  46. if (data) {
  47. struct nvbios_boostS boostS;
  48. u8 idx = 0, sver, shdr;
  49. u32 subd;
  50. input = max(boostE.min, input);
  51. input = min(boostE.max, input);
  52. do {
  53. sver = ver;
  54. shdr = hdr;
  55. subd = nvbios_boostSp(bios, idx++, data, &sver, &shdr,
  56. cnt, len, &boostS);
  57. if (subd && boostS.domain == domain) {
  58. if (adjust)
  59. input = input * boostS.percent / 100;
  60. input = max(boostS.min, input);
  61. input = min(boostS.max, input);
  62. break;
  63. }
  64. } while (subd);
  65. }
  66. return input;
  67. }
  68. /******************************************************************************
  69. * C-States
  70. *****************************************************************************/
  71. static bool
  72. nvkm_cstate_valid(struct nvkm_clk *clk, struct nvkm_cstate *cstate,
  73. u32 max_volt, int temp)
  74. {
  75. const struct nvkm_domain *domain = clk->domains;
  76. struct nvkm_volt *volt = clk->subdev.device->volt;
  77. int voltage;
  78. while (domain && domain->name != nv_clk_src_max) {
  79. if (domain->flags & NVKM_CLK_DOM_FLAG_VPSTATE) {
  80. u32 freq = cstate->domain[domain->name];
  81. switch (clk->boost_mode) {
  82. case NVKM_CLK_BOOST_NONE:
  83. if (clk->base_khz && freq > clk->base_khz)
  84. return false;
  85. case NVKM_CLK_BOOST_BIOS:
  86. if (clk->boost_khz && freq > clk->boost_khz)
  87. return false;
  88. }
  89. }
  90. domain++;
  91. }
  92. if (!volt)
  93. return true;
  94. voltage = nvkm_volt_map(volt, cstate->voltage, temp);
  95. if (voltage < 0)
  96. return false;
  97. return voltage <= min(max_volt, volt->max_uv);
  98. }
  99. static struct nvkm_cstate *
  100. nvkm_cstate_find_best(struct nvkm_clk *clk, struct nvkm_pstate *pstate,
  101. struct nvkm_cstate *cstate)
  102. {
  103. struct nvkm_device *device = clk->subdev.device;
  104. struct nvkm_volt *volt = device->volt;
  105. int max_volt;
  106. if (!pstate || !cstate)
  107. return NULL;
  108. if (!volt)
  109. return cstate;
  110. max_volt = volt->max_uv;
  111. if (volt->max0_id != 0xff)
  112. max_volt = min(max_volt,
  113. nvkm_volt_map(volt, volt->max0_id, clk->temp));
  114. if (volt->max1_id != 0xff)
  115. max_volt = min(max_volt,
  116. nvkm_volt_map(volt, volt->max1_id, clk->temp));
  117. if (volt->max2_id != 0xff)
  118. max_volt = min(max_volt,
  119. nvkm_volt_map(volt, volt->max2_id, clk->temp));
  120. list_for_each_entry_from_reverse(cstate, &pstate->list, head) {
  121. if (nvkm_cstate_valid(clk, cstate, max_volt, clk->temp))
  122. break;
  123. }
  124. return cstate;
  125. }
  126. static struct nvkm_cstate *
  127. nvkm_cstate_get(struct nvkm_clk *clk, struct nvkm_pstate *pstate, int cstatei)
  128. {
  129. struct nvkm_cstate *cstate;
  130. if (cstatei == NVKM_CLK_CSTATE_HIGHEST)
  131. return list_last_entry(&pstate->list, typeof(*cstate), head);
  132. else {
  133. list_for_each_entry(cstate, &pstate->list, head) {
  134. if (cstate->id == cstatei)
  135. return cstate;
  136. }
  137. }
  138. return NULL;
  139. }
  140. static int
  141. nvkm_cstate_prog(struct nvkm_clk *clk, struct nvkm_pstate *pstate, int cstatei)
  142. {
  143. struct nvkm_subdev *subdev = &clk->subdev;
  144. struct nvkm_device *device = subdev->device;
  145. struct nvkm_therm *therm = device->therm;
  146. struct nvkm_volt *volt = device->volt;
  147. struct nvkm_cstate *cstate;
  148. int ret;
  149. if (!list_empty(&pstate->list)) {
  150. cstate = nvkm_cstate_get(clk, pstate, cstatei);
  151. cstate = nvkm_cstate_find_best(clk, pstate, cstate);
  152. } else {
  153. cstate = &pstate->base;
  154. }
  155. if (therm) {
  156. ret = nvkm_therm_cstate(therm, pstate->fanspeed, +1);
  157. if (ret && ret != -ENODEV) {
  158. nvkm_error(subdev, "failed to raise fan speed: %d\n", ret);
  159. return ret;
  160. }
  161. }
  162. if (volt) {
  163. ret = nvkm_volt_set_id(volt, cstate->voltage,
  164. pstate->base.voltage, clk->temp, +1);
  165. if (ret && ret != -ENODEV) {
  166. nvkm_error(subdev, "failed to raise voltage: %d\n", ret);
  167. return ret;
  168. }
  169. }
  170. ret = clk->func->calc(clk, cstate);
  171. if (ret == 0) {
  172. ret = clk->func->prog(clk);
  173. clk->func->tidy(clk);
  174. }
  175. if (volt) {
  176. ret = nvkm_volt_set_id(volt, cstate->voltage,
  177. pstate->base.voltage, clk->temp, -1);
  178. if (ret && ret != -ENODEV)
  179. nvkm_error(subdev, "failed to lower voltage: %d\n", ret);
  180. }
  181. if (therm) {
  182. ret = nvkm_therm_cstate(therm, pstate->fanspeed, -1);
  183. if (ret && ret != -ENODEV)
  184. nvkm_error(subdev, "failed to lower fan speed: %d\n", ret);
  185. }
  186. return ret;
  187. }
  188. static void
  189. nvkm_cstate_del(struct nvkm_cstate *cstate)
  190. {
  191. list_del(&cstate->head);
  192. kfree(cstate);
  193. }
  194. static int
  195. nvkm_cstate_new(struct nvkm_clk *clk, int idx, struct nvkm_pstate *pstate)
  196. {
  197. struct nvkm_bios *bios = clk->subdev.device->bios;
  198. struct nvkm_volt *volt = clk->subdev.device->volt;
  199. const struct nvkm_domain *domain = clk->domains;
  200. struct nvkm_cstate *cstate = NULL;
  201. struct nvbios_cstepX cstepX;
  202. u8 ver, hdr;
  203. u32 data;
  204. data = nvbios_cstepXp(bios, idx, &ver, &hdr, &cstepX);
  205. if (!data)
  206. return -ENOENT;
  207. if (volt && nvkm_volt_map_min(volt, cstepX.voltage) > volt->max_uv)
  208. return -EINVAL;
  209. cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
  210. if (!cstate)
  211. return -ENOMEM;
  212. *cstate = pstate->base;
  213. cstate->voltage = cstepX.voltage;
  214. cstate->id = idx;
  215. while (domain && domain->name != nv_clk_src_max) {
  216. if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
  217. u32 freq = nvkm_clk_adjust(clk, true, pstate->pstate,
  218. domain->bios, cstepX.freq);
  219. cstate->domain[domain->name] = freq;
  220. }
  221. domain++;
  222. }
  223. list_add(&cstate->head, &pstate->list);
  224. return 0;
  225. }
  226. /******************************************************************************
  227. * P-States
  228. *****************************************************************************/
  229. static int
  230. nvkm_pstate_prog(struct nvkm_clk *clk, int pstatei)
  231. {
  232. struct nvkm_subdev *subdev = &clk->subdev;
  233. struct nvkm_fb *fb = subdev->device->fb;
  234. struct nvkm_pci *pci = subdev->device->pci;
  235. struct nvkm_pstate *pstate;
  236. int ret, idx = 0;
  237. list_for_each_entry(pstate, &clk->states, head) {
  238. if (idx++ == pstatei)
  239. break;
  240. }
  241. nvkm_debug(subdev, "setting performance state %d\n", pstatei);
  242. clk->pstate = pstatei;
  243. nvkm_pcie_set_link(pci, pstate->pcie_speed, pstate->pcie_width);
  244. if (fb && fb->ram && fb->ram->func->calc) {
  245. struct nvkm_ram *ram = fb->ram;
  246. int khz = pstate->base.domain[nv_clk_src_mem];
  247. do {
  248. ret = ram->func->calc(ram, khz);
  249. if (ret == 0)
  250. ret = ram->func->prog(ram);
  251. } while (ret > 0);
  252. ram->func->tidy(ram);
  253. }
  254. return nvkm_cstate_prog(clk, pstate, NVKM_CLK_CSTATE_HIGHEST);
  255. }
  256. static void
  257. nvkm_pstate_work(struct work_struct *work)
  258. {
  259. struct nvkm_clk *clk = container_of(work, typeof(*clk), work);
  260. struct nvkm_subdev *subdev = &clk->subdev;
  261. int pstate;
  262. if (!atomic_xchg(&clk->waiting, 0))
  263. return;
  264. clk->pwrsrc = power_supply_is_system_supplied();
  265. nvkm_trace(subdev, "P %d PWR %d U(AC) %d U(DC) %d A %d T %d°C D %d\n",
  266. clk->pstate, clk->pwrsrc, clk->ustate_ac, clk->ustate_dc,
  267. clk->astate, clk->temp, clk->dstate);
  268. pstate = clk->pwrsrc ? clk->ustate_ac : clk->ustate_dc;
  269. if (clk->state_nr && pstate != -1) {
  270. pstate = (pstate < 0) ? clk->astate : pstate;
  271. pstate = min(pstate, clk->state_nr - 1);
  272. pstate = max(pstate, clk->dstate);
  273. } else {
  274. pstate = clk->pstate = -1;
  275. }
  276. nvkm_trace(subdev, "-> %d\n", pstate);
  277. if (pstate != clk->pstate) {
  278. int ret = nvkm_pstate_prog(clk, pstate);
  279. if (ret) {
  280. nvkm_error(subdev, "error setting pstate %d: %d\n",
  281. pstate, ret);
  282. }
  283. }
  284. wake_up_all(&clk->wait);
  285. nvkm_notify_get(&clk->pwrsrc_ntfy);
  286. }
  287. static int
  288. nvkm_pstate_calc(struct nvkm_clk *clk, bool wait)
  289. {
  290. atomic_set(&clk->waiting, 1);
  291. schedule_work(&clk->work);
  292. if (wait)
  293. wait_event(clk->wait, !atomic_read(&clk->waiting));
  294. return 0;
  295. }
  296. static void
  297. nvkm_pstate_info(struct nvkm_clk *clk, struct nvkm_pstate *pstate)
  298. {
  299. const struct nvkm_domain *clock = clk->domains - 1;
  300. struct nvkm_cstate *cstate;
  301. struct nvkm_subdev *subdev = &clk->subdev;
  302. char info[3][32] = { "", "", "" };
  303. char name[4] = "--";
  304. int i = -1;
  305. if (pstate->pstate != 0xff)
  306. snprintf(name, sizeof(name), "%02x", pstate->pstate);
  307. while ((++clock)->name != nv_clk_src_max) {
  308. u32 lo = pstate->base.domain[clock->name];
  309. u32 hi = lo;
  310. if (hi == 0)
  311. continue;
  312. nvkm_debug(subdev, "%02x: %10d KHz\n", clock->name, lo);
  313. list_for_each_entry(cstate, &pstate->list, head) {
  314. u32 freq = cstate->domain[clock->name];
  315. lo = min(lo, freq);
  316. hi = max(hi, freq);
  317. nvkm_debug(subdev, "%10d KHz\n", freq);
  318. }
  319. if (clock->mname && ++i < ARRAY_SIZE(info)) {
  320. lo /= clock->mdiv;
  321. hi /= clock->mdiv;
  322. if (lo == hi) {
  323. snprintf(info[i], sizeof(info[i]), "%s %d MHz",
  324. clock->mname, lo);
  325. } else {
  326. snprintf(info[i], sizeof(info[i]),
  327. "%s %d-%d MHz", clock->mname, lo, hi);
  328. }
  329. }
  330. }
  331. nvkm_debug(subdev, "%s: %s %s %s\n", name, info[0], info[1], info[2]);
  332. }
  333. static void
  334. nvkm_pstate_del(struct nvkm_pstate *pstate)
  335. {
  336. struct nvkm_cstate *cstate, *temp;
  337. list_for_each_entry_safe(cstate, temp, &pstate->list, head) {
  338. nvkm_cstate_del(cstate);
  339. }
  340. list_del(&pstate->head);
  341. kfree(pstate);
  342. }
  343. static int
  344. nvkm_pstate_new(struct nvkm_clk *clk, int idx)
  345. {
  346. struct nvkm_bios *bios = clk->subdev.device->bios;
  347. const struct nvkm_domain *domain = clk->domains - 1;
  348. struct nvkm_pstate *pstate;
  349. struct nvkm_cstate *cstate;
  350. struct nvbios_cstepE cstepE;
  351. struct nvbios_perfE perfE;
  352. u8 ver, hdr, cnt, len;
  353. u32 data;
  354. data = nvbios_perfEp(bios, idx, &ver, &hdr, &cnt, &len, &perfE);
  355. if (!data)
  356. return -EINVAL;
  357. if (perfE.pstate == 0xff)
  358. return 0;
  359. pstate = kzalloc(sizeof(*pstate), GFP_KERNEL);
  360. cstate = &pstate->base;
  361. if (!pstate)
  362. return -ENOMEM;
  363. INIT_LIST_HEAD(&pstate->list);
  364. pstate->pstate = perfE.pstate;
  365. pstate->fanspeed = perfE.fanspeed;
  366. pstate->pcie_speed = perfE.pcie_speed;
  367. pstate->pcie_width = perfE.pcie_width;
  368. cstate->voltage = perfE.voltage;
  369. cstate->domain[nv_clk_src_core] = perfE.core;
  370. cstate->domain[nv_clk_src_shader] = perfE.shader;
  371. cstate->domain[nv_clk_src_mem] = perfE.memory;
  372. cstate->domain[nv_clk_src_vdec] = perfE.vdec;
  373. cstate->domain[nv_clk_src_dom6] = perfE.disp;
  374. while (ver >= 0x40 && (++domain)->name != nv_clk_src_max) {
  375. struct nvbios_perfS perfS;
  376. u8 sver = ver, shdr = hdr;
  377. u32 perfSe = nvbios_perfSp(bios, data, domain->bios,
  378. &sver, &shdr, cnt, len, &perfS);
  379. if (perfSe == 0 || sver != 0x40)
  380. continue;
  381. if (domain->flags & NVKM_CLK_DOM_FLAG_CORE) {
  382. perfS.v40.freq = nvkm_clk_adjust(clk, false,
  383. pstate->pstate,
  384. domain->bios,
  385. perfS.v40.freq);
  386. }
  387. cstate->domain[domain->name] = perfS.v40.freq;
  388. }
  389. data = nvbios_cstepEm(bios, pstate->pstate, &ver, &hdr, &cstepE);
  390. if (data) {
  391. int idx = cstepE.index;
  392. do {
  393. nvkm_cstate_new(clk, idx, pstate);
  394. } while(idx--);
  395. }
  396. nvkm_pstate_info(clk, pstate);
  397. list_add_tail(&pstate->head, &clk->states);
  398. clk->state_nr++;
  399. return 0;
  400. }
  401. /******************************************************************************
  402. * Adjustment triggers
  403. *****************************************************************************/
  404. static int
  405. nvkm_clk_ustate_update(struct nvkm_clk *clk, int req)
  406. {
  407. struct nvkm_pstate *pstate;
  408. int i = 0;
  409. if (!clk->allow_reclock)
  410. return -ENOSYS;
  411. if (req != -1 && req != -2) {
  412. list_for_each_entry(pstate, &clk->states, head) {
  413. if (pstate->pstate == req)
  414. break;
  415. i++;
  416. }
  417. if (pstate->pstate != req)
  418. return -EINVAL;
  419. req = i;
  420. }
  421. return req + 2;
  422. }
  423. static int
  424. nvkm_clk_nstate(struct nvkm_clk *clk, const char *mode, int arglen)
  425. {
  426. int ret = 1;
  427. if (clk->allow_reclock && !strncasecmpz(mode, "auto", arglen))
  428. return -2;
  429. if (strncasecmpz(mode, "disabled", arglen)) {
  430. char save = mode[arglen];
  431. long v;
  432. ((char *)mode)[arglen] = '\0';
  433. if (!kstrtol(mode, 0, &v)) {
  434. ret = nvkm_clk_ustate_update(clk, v);
  435. if (ret < 0)
  436. ret = 1;
  437. }
  438. ((char *)mode)[arglen] = save;
  439. }
  440. return ret - 2;
  441. }
  442. int
  443. nvkm_clk_ustate(struct nvkm_clk *clk, int req, int pwr)
  444. {
  445. int ret = nvkm_clk_ustate_update(clk, req);
  446. if (ret >= 0) {
  447. if (ret -= 2, pwr) clk->ustate_ac = ret;
  448. else clk->ustate_dc = ret;
  449. return nvkm_pstate_calc(clk, true);
  450. }
  451. return ret;
  452. }
  453. int
  454. nvkm_clk_astate(struct nvkm_clk *clk, int req, int rel, bool wait)
  455. {
  456. if (!rel) clk->astate = req;
  457. if ( rel) clk->astate += rel;
  458. clk->astate = min(clk->astate, clk->state_nr - 1);
  459. clk->astate = max(clk->astate, 0);
  460. return nvkm_pstate_calc(clk, wait);
  461. }
  462. int
  463. nvkm_clk_tstate(struct nvkm_clk *clk, u8 temp)
  464. {
  465. if (clk->temp == temp)
  466. return 0;
  467. clk->temp = temp;
  468. return nvkm_pstate_calc(clk, false);
  469. }
  470. int
  471. nvkm_clk_dstate(struct nvkm_clk *clk, int req, int rel)
  472. {
  473. if (!rel) clk->dstate = req;
  474. if ( rel) clk->dstate += rel;
  475. clk->dstate = min(clk->dstate, clk->state_nr - 1);
  476. clk->dstate = max(clk->dstate, 0);
  477. return nvkm_pstate_calc(clk, true);
  478. }
  479. static int
  480. nvkm_clk_pwrsrc(struct nvkm_notify *notify)
  481. {
  482. struct nvkm_clk *clk =
  483. container_of(notify, typeof(*clk), pwrsrc_ntfy);
  484. nvkm_pstate_calc(clk, false);
  485. return NVKM_NOTIFY_DROP;
  486. }
  487. /******************************************************************************
  488. * subdev base class implementation
  489. *****************************************************************************/
  490. int
  491. nvkm_clk_read(struct nvkm_clk *clk, enum nv_clk_src src)
  492. {
  493. return clk->func->read(clk, src);
  494. }
  495. static int
  496. nvkm_clk_fini(struct nvkm_subdev *subdev, bool suspend)
  497. {
  498. struct nvkm_clk *clk = nvkm_clk(subdev);
  499. nvkm_notify_put(&clk->pwrsrc_ntfy);
  500. flush_work(&clk->work);
  501. if (clk->func->fini)
  502. clk->func->fini(clk);
  503. return 0;
  504. }
  505. static int
  506. nvkm_clk_init(struct nvkm_subdev *subdev)
  507. {
  508. struct nvkm_clk *clk = nvkm_clk(subdev);
  509. const struct nvkm_domain *clock = clk->domains;
  510. int ret;
  511. memset(&clk->bstate, 0x00, sizeof(clk->bstate));
  512. INIT_LIST_HEAD(&clk->bstate.list);
  513. clk->bstate.pstate = 0xff;
  514. while (clock->name != nv_clk_src_max) {
  515. ret = nvkm_clk_read(clk, clock->name);
  516. if (ret < 0) {
  517. nvkm_error(subdev, "%02x freq unknown\n", clock->name);
  518. return ret;
  519. }
  520. clk->bstate.base.domain[clock->name] = ret;
  521. clock++;
  522. }
  523. nvkm_pstate_info(clk, &clk->bstate);
  524. if (clk->func->init)
  525. return clk->func->init(clk);
  526. clk->astate = clk->state_nr - 1;
  527. clk->dstate = 0;
  528. clk->pstate = -1;
  529. clk->temp = 90; /* reasonable default value */
  530. nvkm_pstate_calc(clk, true);
  531. return 0;
  532. }
  533. static void *
  534. nvkm_clk_dtor(struct nvkm_subdev *subdev)
  535. {
  536. struct nvkm_clk *clk = nvkm_clk(subdev);
  537. struct nvkm_pstate *pstate, *temp;
  538. nvkm_notify_fini(&clk->pwrsrc_ntfy);
  539. /* Early return if the pstates have been provided statically */
  540. if (clk->func->pstates)
  541. return clk;
  542. list_for_each_entry_safe(pstate, temp, &clk->states, head) {
  543. nvkm_pstate_del(pstate);
  544. }
  545. return clk;
  546. }
  547. static const struct nvkm_subdev_func
  548. nvkm_clk = {
  549. .dtor = nvkm_clk_dtor,
  550. .init = nvkm_clk_init,
  551. .fini = nvkm_clk_fini,
  552. };
  553. int
  554. nvkm_clk_ctor(const struct nvkm_clk_func *func, struct nvkm_device *device,
  555. int index, bool allow_reclock, struct nvkm_clk *clk)
  556. {
  557. struct nvkm_subdev *subdev = &clk->subdev;
  558. struct nvkm_bios *bios = device->bios;
  559. int ret, idx, arglen;
  560. const char *mode;
  561. struct nvbios_vpstate_header h;
  562. nvkm_subdev_ctor(&nvkm_clk, device, index, subdev);
  563. if (bios && !nvbios_vpstate_parse(bios, &h)) {
  564. struct nvbios_vpstate_entry base, boost;
  565. if (!nvbios_vpstate_entry(bios, &h, h.boost_id, &boost))
  566. clk->boost_khz = boost.clock_mhz * 1000;
  567. if (!nvbios_vpstate_entry(bios, &h, h.base_id, &base))
  568. clk->base_khz = base.clock_mhz * 1000;
  569. }
  570. clk->func = func;
  571. INIT_LIST_HEAD(&clk->states);
  572. clk->domains = func->domains;
  573. clk->ustate_ac = -1;
  574. clk->ustate_dc = -1;
  575. clk->allow_reclock = allow_reclock;
  576. INIT_WORK(&clk->work, nvkm_pstate_work);
  577. init_waitqueue_head(&clk->wait);
  578. atomic_set(&clk->waiting, 0);
  579. /* If no pstates are provided, try and fetch them from the BIOS */
  580. if (!func->pstates) {
  581. idx = 0;
  582. do {
  583. ret = nvkm_pstate_new(clk, idx++);
  584. } while (ret == 0);
  585. } else {
  586. for (idx = 0; idx < func->nr_pstates; idx++)
  587. list_add_tail(&func->pstates[idx].head, &clk->states);
  588. clk->state_nr = func->nr_pstates;
  589. }
  590. ret = nvkm_notify_init(NULL, &device->event, nvkm_clk_pwrsrc, true,
  591. NULL, 0, 0, &clk->pwrsrc_ntfy);
  592. if (ret)
  593. return ret;
  594. mode = nvkm_stropt(device->cfgopt, "NvClkMode", &arglen);
  595. if (mode) {
  596. clk->ustate_ac = nvkm_clk_nstate(clk, mode, arglen);
  597. clk->ustate_dc = nvkm_clk_nstate(clk, mode, arglen);
  598. }
  599. mode = nvkm_stropt(device->cfgopt, "NvClkModeAC", &arglen);
  600. if (mode)
  601. clk->ustate_ac = nvkm_clk_nstate(clk, mode, arglen);
  602. mode = nvkm_stropt(device->cfgopt, "NvClkModeDC", &arglen);
  603. if (mode)
  604. clk->ustate_dc = nvkm_clk_nstate(clk, mode, arglen);
  605. clk->boost_mode = nvkm_longopt(device->cfgopt, "NvBoost",
  606. NVKM_CLK_BOOST_NONE);
  607. return 0;
  608. }
  609. int
  610. nvkm_clk_new_(const struct nvkm_clk_func *func, struct nvkm_device *device,
  611. int index, bool allow_reclock, struct nvkm_clk **pclk)
  612. {
  613. if (!(*pclk = kzalloc(sizeof(**pclk), GFP_KERNEL)))
  614. return -ENOMEM;
  615. return nvkm_clk_ctor(func, device, index, allow_reclock, *pclk);
  616. }