Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 25 | #include <drm/drmP.h> |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 26 | #include "nouveau_drm.h" |
| 27 | #include "nouveau_reg.h" |
Ben Skeggs | 1a64634 | 2013-03-21 15:45:11 +1000 | [diff] [blame^] | 28 | #include "dispnv04/hw.h" |
Francisco Jerez | 5c4abd0 | 2010-09-23 20:36:42 +0200 | [diff] [blame] | 29 | #include "nouveau_pm.h" |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 30 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 31 | #include <subdev/bios/pll.h> |
| 32 | #include <subdev/clock.h> |
| 33 | #include <subdev/timer.h> |
| 34 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 35 | int |
| 36 | nv04_pm_clocks_get(struct drm_device *dev, struct nouveau_pm_level *perflvl) |
| 37 | { |
| 38 | int ret; |
| 39 | |
| 40 | ret = nouveau_hw_get_clock(dev, PLL_CORE); |
| 41 | if (ret < 0) |
| 42 | return ret; |
| 43 | perflvl->core = ret; |
| 44 | |
| 45 | ret = nouveau_hw_get_clock(dev, PLL_MEMORY); |
| 46 | if (ret < 0) |
| 47 | return ret; |
| 48 | perflvl->memory = ret; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | struct nv04_pm_clock { |
Ben Skeggs | 70790f4 | 2012-07-10 17:26:46 +1000 | [diff] [blame] | 54 | struct nvbios_pll pll; |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 55 | struct nouveau_pll_vals calc; |
| 56 | }; |
| 57 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 58 | struct nv04_pm_state { |
| 59 | struct nv04_pm_clock core; |
| 60 | struct nv04_pm_clock memory; |
| 61 | }; |
| 62 | |
| 63 | static int |
| 64 | calc_pll(struct drm_device *dev, u32 id, int khz, struct nv04_pm_clock *clk) |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 65 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 66 | struct nouveau_device *device = nouveau_dev(dev); |
| 67 | struct nouveau_bios *bios = nouveau_bios(device); |
| 68 | struct nouveau_clock *pclk = nouveau_clock(device); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 69 | int ret; |
| 70 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 71 | ret = nvbios_pll_parse(bios, id, &clk->pll); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
| 74 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 75 | ret = pclk->pll_calc(pclk, &clk->pll, khz, &clk->calc); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 76 | if (!ret) |
| 77 | return -EINVAL; |
| 78 | |
| 79 | return 0; |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void * |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 83 | nv04_pm_clocks_pre(struct drm_device *dev, struct nouveau_pm_level *perflvl) |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 84 | { |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 85 | struct nv04_pm_state *info; |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 86 | int ret; |
| 87 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 88 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 89 | if (!info) |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 90 | return ERR_PTR(-ENOMEM); |
| 91 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 92 | ret = calc_pll(dev, PLL_CORE, perflvl->core, &info->core); |
| 93 | if (ret) |
| 94 | goto error; |
| 95 | |
| 96 | if (perflvl->memory) { |
| 97 | ret = calc_pll(dev, PLL_MEMORY, perflvl->memory, &info->memory); |
| 98 | if (ret) |
| 99 | goto error; |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 100 | } |
| 101 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 102 | return info; |
| 103 | error: |
| 104 | kfree(info); |
| 105 | return ERR_PTR(ret); |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 106 | } |
| 107 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 108 | static void |
| 109 | prog_pll(struct drm_device *dev, struct nv04_pm_clock *clk) |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 110 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 111 | struct nouveau_device *device = nouveau_dev(dev); |
| 112 | struct nouveau_clock *pclk = nouveau_clock(device); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 113 | u32 reg = clk->pll.reg; |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 114 | |
| 115 | /* thank the insane nouveau_hw_setpll() interface for this */ |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 116 | if (device->card_type >= NV_40) |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 117 | reg += 4; |
| 118 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 119 | pclk->pll_prog(pclk, reg, &clk->calc); |
Ben Skeggs | 442b626 | 2010-09-16 16:25:26 +1000 | [diff] [blame] | 120 | } |
| 121 | |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 122 | int |
| 123 | nv04_pm_clocks_set(struct drm_device *dev, void *pre_state) |
| 124 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 125 | struct nouveau_device *device = nouveau_dev(dev); |
| 126 | struct nouveau_timer *ptimer = nouveau_timer(device); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 127 | struct nv04_pm_state *state = pre_state; |
| 128 | |
| 129 | prog_pll(dev, &state->core); |
| 130 | |
| 131 | if (state->memory.pll.reg) { |
| 132 | prog_pll(dev, &state->memory); |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 133 | if (device->card_type < NV_30) { |
| 134 | if (device->card_type == NV_20) |
| 135 | nv_mask(device, 0x1002c4, 0, 1 << 20); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 136 | |
| 137 | /* Reset the DLLs */ |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 138 | nv_mask(device, 0x1002c0, 0, 1 << 8); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame] | 142 | nv_ofuncs(ptimer)->init(nv_object(ptimer)); |
Ben Skeggs | 36f1317 | 2011-10-27 10:24:12 +1000 | [diff] [blame] | 143 | |
| 144 | kfree(state); |
| 145 | return 0; |
| 146 | } |