-
Notifications
You must be signed in to change notification settings - Fork 1
/
apple_gmux.c
142 lines (128 loc) · 3.64 KB
/
apple_gmux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* apple_gmux.c - test module for dynamic loading */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2007 Free Software Foundation, Inc.
* Copyright (C) 2003 NIIBE Yutaka <[email protected]>
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
#include <grub/cpu/io.h>
#include <grub/time.h>
GRUB_MOD_LICENSE ("GPLv3+");
#define GMUX_PORT_SWITCH_DISPLAY 0x10
#define GMUX_PORT_SWITCH_DDC 0x28
#define GMUX_PORT_SWITCH_EXTERNAL 0x40
#define GMUX_PORT_DISCRETE_POWER 0x50
#define GMUX_PORT_VALUE 0xc2
#define GMUX_PORT_READ 0xd0
#define GMUX_PORT_WRITE 0xd4
#define GMUX_IOSTART 0x700
typedef unsigned char u8;
enum discrete_state {STATE_ON, STATE_OFF};
enum gpu_id {IGD, DIS};
static int index_wait_ready(void)
{
int i = 200;
u8 gwr = grub_inb(GMUX_IOSTART + GMUX_PORT_WRITE);
while (i && (gwr & 0x01)) {
grub_inb(GMUX_IOSTART + GMUX_PORT_READ);
gwr = grub_inb(GMUX_IOSTART + GMUX_PORT_WRITE);
grub_millisleep(100);
i--;
}
return !!i;
}
static int index_wait_complete(void)
{
int i = 200;
u8 gwr = grub_inb(GMUX_IOSTART + GMUX_PORT_WRITE);
while (i && !(gwr & 0x01)) {
gwr = grub_inb(GMUX_IOSTART + GMUX_PORT_WRITE);
grub_millisleep(100);
i--;
}
if (gwr & 0x01)
grub_inb(GMUX_IOSTART + GMUX_PORT_READ);
return !!i;
}
static void index_write8(int port, u8 val)
{
grub_outb(val, GMUX_IOSTART + GMUX_PORT_VALUE);
index_wait_ready();
grub_outb((port & 0xff), GMUX_IOSTART + GMUX_PORT_WRITE);
index_wait_complete();
}
/*static u8 index_read8(int port)
{
u8 val;
index_wait_ready();
grub_outb((port & 0xff), GMUX_IOSTART + GMUX_PORT_READ);
index_wait_complete();
val = grub_inb(GMUX_IOSTART + GMUX_PORT_VALUE);
return val;
}
*/
static void set_discrete_state(enum discrete_state state)
{
if (state == STATE_ON) { // switch on dGPU
index_write8(GMUX_PORT_DISCRETE_POWER, 1);
index_write8(GMUX_PORT_DISCRETE_POWER, 3);
} else { // switch off dGPU
index_write8(GMUX_PORT_DISCRETE_POWER, 1);
index_write8(GMUX_PORT_DISCRETE_POWER, 0);
}
}
/* static u8 get_discrete_state()
{
return index_read8(GMUX_PORT_DISCRETE_POWER);
}
*/
static void switchto(enum gpu_id id)
{
if (id == IGD) { // switch to iGPU
index_write8(GMUX_PORT_SWITCH_DDC, 1);
index_write8(GMUX_PORT_SWITCH_DISPLAY, 2);
index_write8(GMUX_PORT_SWITCH_EXTERNAL, 2);
} else { // switch to dGPU
index_write8(GMUX_PORT_SWITCH_DDC, 2);
index_write8(GMUX_PORT_SWITCH_DISPLAY, 3);
index_write8(GMUX_PORT_SWITCH_EXTERNAL, 3);
}
}
static grub_err_t
grub_cmd_apple_gmux (grub_extcmd_context_t ctxt __attribute__ ((unused)),
int argc __attribute__ ((unused)),
char **args __attribute__ ((unused)))
{
switchto(IGD);
set_discrete_state(STATE_OFF);
return 0;
}
static grub_extcmd_t cmd;
GRUB_MOD_INIT(apple_gmux)
{
cmd = grub_register_extcmd ("apple_gmux", grub_cmd_apple_gmux, 0, 0,
N_("Turns off Apple Discrete Grphics Card."), 0);
}
GRUB_MOD_FINI(apple_gmux)
{
grub_unregister_extcmd (cmd);
}