It’s called “head pressing” and it occurs in dogs and cats.
Head pressing is characterized by the compulsive act of pressing the head against a wall or other object for no apparent reason. This generally indicates damage to the nervous system, which may result from a number of varying causes, including prosencephalon disease (in which the forebrain and thalamusparts of the brain are damaged), or toxic poisoning.