9
votes

I have wondered for quite some time how CPU instructions can interact with the GPU. As I understand things, the CPU has a certain set of instructions (machine code) that it understands and performs and a driver is a piece of software that communicates via the CPU to the GPU.

But how does this piece of software communicate? Does the CPU contain certain assembly instructions that explicitly tells it to communicate to another device?

Can I write assembly code or C/C++ code to communicate to the Graphics Card just like the driver does given one specific machine enviroment?

5
You know there are open source operating systems, right? Just dive in. It's deep waters, but very educational.unwind
How does the CPU communicate with a hard drive, a mouse or keyboard, a power button, or a sound chip?user253751

5 Answers

12
votes

Like any hardware device on a PC the graphics card will respond to reads and writes to certain memory addresses, and possibly input/output ports. The PCI bus defines how these are allocated.

There are no specific CPU instructions to communicate with graphics cards, in the case of writing to memory locations it just uses the ordinary instructions to do that and in the case of port IO it just uses the generic instructions to do that. In both cases there will be some CPU setup needed to "map" the memory locations into virtual address space or to allow access to the ports.

A write to memory location 1234567 could be directed to the graphics card to indicate a command to it for example. (That's just a made up example of course) You certainly could write your own driver that did just that, however you'd have to know exactly what the card expected in order to different things, and that generally is a secret only known to the manufacturer and they implement it in their driver software. Some cards are better documented than others, and some have been partially reverse engineered.

4
votes

Yeah, the memory address are mapped to each devices in the computer system. The basic thing is if you write to certain memory area that is mapped tot he device, the device gets written. for that the device has specific registers and other programmable spaces mapped to main memory. Using these registers you configure the devices like GPU for your case. And finally for huge data transfer obviously the GPU used a DMA transfer so it can work without intervention of the CPU once DMA transfer is initiated.

So bottom line is, To communicate with GPU or other hardware devices you need to study the system architecture where the GPU is installed because it is finally the system software receding in the system where GPU is installed responsible all kind of memory mapping and allocations.

2
votes

The video card is no more magical than any other item inside or outside the computer. How are you reading this web page, you have at least two computers communicating with each other over an interface (ethernet/internet). Memory, pcie (video, hard disk, usb, etc), usb (mouse, keyboard, etc), network, are all connected to the main cpu via interfaces that are ultimately tied to the cpu's external interface (memory bus for lack of a better term).

We like to think that many of these devices are pure logic and that seems to make sense that when you write to some address that is routed to that peripheral that address and data tells that blob of logic to perform some action. But just like reading this web page on one computer that is served up by some other computer, it doesnt have to be pure logic, you just need an interface to move data. Not only a gpu but a number of other things in your computer have processors inside, the network interfaces might, the mouse and keyboard probably do, hard disk most likely, and as we now know the battery packs on laptops have a processor with firmware as well (mac batteries being infected with a virus that re-infects the computer after a clean re-install).

No magic. The software/firmware running on these other processors in your computer are not unlike the logic, a percentage of their job is to wait for commands from the main cpu to do things. Mouse please give me the button status. Gpu please draw me a triangle.

There are no special assembly language instructions in the cpu for video. The cpu doesnt know a video card from a hole in the wall, the driver knows/determines the address space for the video card it knows how to talk to, through that address space it writes the data values required to tell the video card to do things. Draw me a triangle may involve several items of data, no doubt the three corner coordinates, perhaps a fill color or is it straight lines which is likely just part of the definition of the communication interface with that peripheral.

There is no magic here, if you wanted to ask someone to do something for you you would provide a list of things for them to do and communicate in a way they could understand (go to 123 main street, ring the doorbell, tell the person that answers you would like two tickets to tonights performance, give them this money, return with the change and the tickets), with hardware it is easier as the communication interface is well defined for that peripheral.

0
votes

in addition to the answers above, CPU contacts and deals with the GPU by sending variables (memory addresses) as values that have meaning inside GPU also perform calls that the graphics APIs (OpenGL, Vulkan...) need to do, those APIs is typically used to interact with the GPUs, to achieve hardware-accelerated rendering (if it's used for rendering and not a GPGPU situation) by passing by many steps one of the basics is the Rendering Pipeline.

-1
votes

As Von Neumann architecture, memory bus is the center of a hardware system. Every device talks to the memory bus, including CPU.

Let's look CPU, which can only issue basic read/write operations to memory bus, and some extra ops, like lock/barrier.

And so do other devices, eg, GPU can read/write main memory by DMA via PCI Interface; and GPU also maps some register to the main bus, write to the specific address where the register mapped can issue specific command.

In general, all devices talk each other through the main memory bus.

But in extra, CPU also has an interrupt interface, which is beyond the question.