i915_gem.c 136 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353
  1. /*
  2. * Copyright © 2008 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/oom.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-buf.h>
  39. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  40. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  41. bool force);
  42. static __must_check int
  43. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  44. bool readonly);
  45. static void
  46. i915_gem_object_retire(struct drm_i915_gem_object *obj);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static unsigned long i915_gem_shrinker_count(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static unsigned long i915_gem_shrinker_scan(struct shrinker *shrinker,
  55. struct shrink_control *sc);
  56. static int i915_gem_shrinker_oom(struct notifier_block *nb,
  57. unsigned long event,
  58. void *ptr);
  59. static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  60. static bool cpu_cache_is_coherent(struct drm_device *dev,
  61. enum i915_cache_level level)
  62. {
  63. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  64. }
  65. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  66. {
  67. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  68. return true;
  69. return obj->pin_display;
  70. }
  71. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  72. {
  73. if (obj->tiling_mode)
  74. i915_gem_release_mmap(obj);
  75. /* As we do not have an associated fence register, we will force
  76. * a tiling change if we ever need to acquire one.
  77. */
  78. obj->fence_dirty = false;
  79. obj->fence_reg = I915_FENCE_REG_NONE;
  80. }
  81. /* some bookkeeping */
  82. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  83. size_t size)
  84. {
  85. spin_lock(&dev_priv->mm.object_stat_lock);
  86. dev_priv->mm.object_count++;
  87. dev_priv->mm.object_memory += size;
  88. spin_unlock(&dev_priv->mm.object_stat_lock);
  89. }
  90. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  91. size_t size)
  92. {
  93. spin_lock(&dev_priv->mm.object_stat_lock);
  94. dev_priv->mm.object_count--;
  95. dev_priv->mm.object_memory -= size;
  96. spin_unlock(&dev_priv->mm.object_stat_lock);
  97. }
  98. static int
  99. i915_gem_wait_for_error(struct i915_gpu_error *error)
  100. {
  101. int ret;
  102. #define EXIT_COND (!i915_reset_in_progress(error) || \
  103. i915_terminally_wedged(error))
  104. if (EXIT_COND)
  105. return 0;
  106. /*
  107. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  108. * userspace. If it takes that long something really bad is going on and
  109. * we should simply try to bail out and fail as gracefully as possible.
  110. */
  111. ret = wait_event_interruptible_timeout(error->reset_queue,
  112. EXIT_COND,
  113. 10*HZ);
  114. if (ret == 0) {
  115. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  116. return -EIO;
  117. } else if (ret < 0) {
  118. return ret;
  119. }
  120. #undef EXIT_COND
  121. return 0;
  122. }
  123. int i915_mutex_lock_interruptible(struct drm_device *dev)
  124. {
  125. struct drm_i915_private *dev_priv = dev->dev_private;
  126. int ret;
  127. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  128. if (ret)
  129. return ret;
  130. ret = mutex_lock_interruptible(&dev->struct_mutex);
  131. if (ret)
  132. return ret;
  133. WARN_ON(i915_verify_lists(dev));
  134. return 0;
  135. }
  136. int
  137. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  138. struct drm_file *file)
  139. {
  140. struct drm_i915_private *dev_priv = dev->dev_private;
  141. struct drm_i915_gem_get_aperture *args = data;
  142. struct drm_i915_gem_object *obj;
  143. size_t pinned;
  144. pinned = 0;
  145. mutex_lock(&dev->struct_mutex);
  146. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  147. if (i915_gem_obj_is_pinned(obj))
  148. pinned += i915_gem_obj_ggtt_size(obj);
  149. mutex_unlock(&dev->struct_mutex);
  150. args->aper_size = dev_priv->gtt.base.total;
  151. args->aper_available_size = args->aper_size - pinned;
  152. return 0;
  153. }
  154. static int
  155. i915_gem_object_get_pages_phys(struct drm_i915_gem_object *obj)
  156. {
  157. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  158. char *vaddr = obj->phys_handle->vaddr;
  159. struct sg_table *st;
  160. struct scatterlist *sg;
  161. int i;
  162. if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj)))
  163. return -EINVAL;
  164. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  165. struct page *page;
  166. char *src;
  167. page = shmem_read_mapping_page(mapping, i);
  168. if (IS_ERR(page))
  169. return PTR_ERR(page);
  170. src = kmap_atomic(page);
  171. memcpy(vaddr, src, PAGE_SIZE);
  172. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  173. kunmap_atomic(src);
  174. page_cache_release(page);
  175. vaddr += PAGE_SIZE;
  176. }
  177. i915_gem_chipset_flush(obj->base.dev);
  178. st = kmalloc(sizeof(*st), GFP_KERNEL);
  179. if (st == NULL)
  180. return -ENOMEM;
  181. if (sg_alloc_table(st, 1, GFP_KERNEL)) {
  182. kfree(st);
  183. return -ENOMEM;
  184. }
  185. sg = st->sgl;
  186. sg->offset = 0;
  187. sg->length = obj->base.size;
  188. sg_dma_address(sg) = obj->phys_handle->busaddr;
  189. sg_dma_len(sg) = obj->base.size;
  190. obj->pages = st;
  191. obj->has_dma_mapping = true;
  192. return 0;
  193. }
  194. static void
  195. i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj)
  196. {
  197. int ret;
  198. BUG_ON(obj->madv == __I915_MADV_PURGED);
  199. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  200. if (ret) {
  201. /* In the event of a disaster, abandon all caches and
  202. * hope for the best.
  203. */
  204. WARN_ON(ret != -EIO);
  205. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  206. }
  207. if (obj->madv == I915_MADV_DONTNEED)
  208. obj->dirty = 0;
  209. if (obj->dirty) {
  210. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  211. char *vaddr = obj->phys_handle->vaddr;
  212. int i;
  213. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  214. struct page *page;
  215. char *dst;
  216. page = shmem_read_mapping_page(mapping, i);
  217. if (IS_ERR(page))
  218. continue;
  219. dst = kmap_atomic(page);
  220. drm_clflush_virt_range(vaddr, PAGE_SIZE);
  221. memcpy(dst, vaddr, PAGE_SIZE);
  222. kunmap_atomic(dst);
  223. set_page_dirty(page);
  224. if (obj->madv == I915_MADV_WILLNEED)
  225. mark_page_accessed(page);
  226. page_cache_release(page);
  227. vaddr += PAGE_SIZE;
  228. }
  229. obj->dirty = 0;
  230. }
  231. sg_free_table(obj->pages);
  232. kfree(obj->pages);
  233. obj->has_dma_mapping = false;
  234. }
  235. static void
  236. i915_gem_object_release_phys(struct drm_i915_gem_object *obj)
  237. {
  238. drm_pci_free(obj->base.dev, obj->phys_handle);
  239. }
  240. static const struct drm_i915_gem_object_ops i915_gem_phys_ops = {
  241. .get_pages = i915_gem_object_get_pages_phys,
  242. .put_pages = i915_gem_object_put_pages_phys,
  243. .release = i915_gem_object_release_phys,
  244. };
  245. static int
  246. drop_pages(struct drm_i915_gem_object *obj)
  247. {
  248. struct i915_vma *vma, *next;
  249. int ret;
  250. drm_gem_object_reference(&obj->base);
  251. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link)
  252. if (i915_vma_unbind(vma))
  253. break;
  254. ret = i915_gem_object_put_pages(obj);
  255. drm_gem_object_unreference(&obj->base);
  256. return ret;
  257. }
  258. int
  259. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  260. int align)
  261. {
  262. drm_dma_handle_t *phys;
  263. int ret;
  264. if (obj->phys_handle) {
  265. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  266. return -EBUSY;
  267. return 0;
  268. }
  269. if (obj->madv != I915_MADV_WILLNEED)
  270. return -EFAULT;
  271. if (obj->base.filp == NULL)
  272. return -EINVAL;
  273. ret = drop_pages(obj);
  274. if (ret)
  275. return ret;
  276. /* create a new object */
  277. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  278. if (!phys)
  279. return -ENOMEM;
  280. obj->phys_handle = phys;
  281. obj->ops = &i915_gem_phys_ops;
  282. return i915_gem_object_get_pages(obj);
  283. }
  284. static int
  285. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  286. struct drm_i915_gem_pwrite *args,
  287. struct drm_file *file_priv)
  288. {
  289. struct drm_device *dev = obj->base.dev;
  290. void *vaddr = obj->phys_handle->vaddr + args->offset;
  291. char __user *user_data = to_user_ptr(args->data_ptr);
  292. int ret;
  293. /* We manually control the domain here and pretend that it
  294. * remains coherent i.e. in the GTT domain, like shmem_pwrite.
  295. */
  296. ret = i915_gem_object_wait_rendering(obj, false);
  297. if (ret)
  298. return ret;
  299. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  300. unsigned long unwritten;
  301. /* The physical object once assigned is fixed for the lifetime
  302. * of the obj, so we can safely drop the lock and continue
  303. * to access vaddr.
  304. */
  305. mutex_unlock(&dev->struct_mutex);
  306. unwritten = copy_from_user(vaddr, user_data, args->size);
  307. mutex_lock(&dev->struct_mutex);
  308. if (unwritten)
  309. return -EFAULT;
  310. }
  311. drm_clflush_virt_range(vaddr, args->size);
  312. i915_gem_chipset_flush(dev);
  313. return 0;
  314. }
  315. void *i915_gem_object_alloc(struct drm_device *dev)
  316. {
  317. struct drm_i915_private *dev_priv = dev->dev_private;
  318. return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL);
  319. }
  320. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  321. {
  322. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  323. kmem_cache_free(dev_priv->slab, obj);
  324. }
  325. static int
  326. i915_gem_create(struct drm_file *file,
  327. struct drm_device *dev,
  328. uint64_t size,
  329. uint32_t *handle_p)
  330. {
  331. struct drm_i915_gem_object *obj;
  332. int ret;
  333. u32 handle;
  334. size = roundup(size, PAGE_SIZE);
  335. if (size == 0)
  336. return -EINVAL;
  337. /* Allocate the new object */
  338. obj = i915_gem_alloc_object(dev, size);
  339. if (obj == NULL)
  340. return -ENOMEM;
  341. ret = drm_gem_handle_create(file, &obj->base, &handle);
  342. /* drop reference from allocate - handle holds it now */
  343. drm_gem_object_unreference_unlocked(&obj->base);
  344. if (ret)
  345. return ret;
  346. *handle_p = handle;
  347. return 0;
  348. }
  349. int
  350. i915_gem_dumb_create(struct drm_file *file,
  351. struct drm_device *dev,
  352. struct drm_mode_create_dumb *args)
  353. {
  354. /* have to work out size/pitch and return them */
  355. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  356. args->size = args->pitch * args->height;
  357. return i915_gem_create(file, dev,
  358. args->size, &args->handle);
  359. }
  360. /**
  361. * Creates a new mm object and returns a handle to it.
  362. */
  363. int
  364. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  365. struct drm_file *file)
  366. {
  367. struct drm_i915_gem_create *args = data;
  368. return i915_gem_create(file, dev,
  369. args->size, &args->handle);
  370. }
  371. static inline int
  372. __copy_to_user_swizzled(char __user *cpu_vaddr,
  373. const char *gpu_vaddr, int gpu_offset,
  374. int length)
  375. {
  376. int ret, cpu_offset = 0;
  377. while (length > 0) {
  378. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  379. int this_length = min(cacheline_end - gpu_offset, length);
  380. int swizzled_gpu_offset = gpu_offset ^ 64;
  381. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  382. gpu_vaddr + swizzled_gpu_offset,
  383. this_length);
  384. if (ret)
  385. return ret + length;
  386. cpu_offset += this_length;
  387. gpu_offset += this_length;
  388. length -= this_length;
  389. }
  390. return 0;
  391. }
  392. static inline int
  393. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  394. const char __user *cpu_vaddr,
  395. int length)
  396. {
  397. int ret, cpu_offset = 0;
  398. while (length > 0) {
  399. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  400. int this_length = min(cacheline_end - gpu_offset, length);
  401. int swizzled_gpu_offset = gpu_offset ^ 64;
  402. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  403. cpu_vaddr + cpu_offset,
  404. this_length);
  405. if (ret)
  406. return ret + length;
  407. cpu_offset += this_length;
  408. gpu_offset += this_length;
  409. length -= this_length;
  410. }
  411. return 0;
  412. }
  413. /*
  414. * Pins the specified object's pages and synchronizes the object with
  415. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  416. * flush the object from the CPU cache.
  417. */
  418. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  419. int *needs_clflush)
  420. {
  421. int ret;
  422. *needs_clflush = 0;
  423. if (!obj->base.filp)
  424. return -EINVAL;
  425. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  426. /* If we're not in the cpu read domain, set ourself into the gtt
  427. * read domain and manually flush cachelines (if required). This
  428. * optimizes for the case when the gpu will dirty the data
  429. * anyway again before the next pread happens. */
  430. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  431. obj->cache_level);
  432. ret = i915_gem_object_wait_rendering(obj, true);
  433. if (ret)
  434. return ret;
  435. i915_gem_object_retire(obj);
  436. }
  437. ret = i915_gem_object_get_pages(obj);
  438. if (ret)
  439. return ret;
  440. i915_gem_object_pin_pages(obj);
  441. return ret;
  442. }
  443. /* Per-page copy function for the shmem pread fastpath.
  444. * Flushes invalid cachelines before reading the target if
  445. * needs_clflush is set. */
  446. static int
  447. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  448. char __user *user_data,
  449. bool page_do_bit17_swizzling, bool needs_clflush)
  450. {
  451. char *vaddr;
  452. int ret;
  453. if (unlikely(page_do_bit17_swizzling))
  454. return -EINVAL;
  455. vaddr = kmap_atomic(page);
  456. if (needs_clflush)
  457. drm_clflush_virt_range(vaddr + shmem_page_offset,
  458. page_length);
  459. ret = __copy_to_user_inatomic(user_data,
  460. vaddr + shmem_page_offset,
  461. page_length);
  462. kunmap_atomic(vaddr);
  463. return ret ? -EFAULT : 0;
  464. }
  465. static void
  466. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  467. bool swizzled)
  468. {
  469. if (unlikely(swizzled)) {
  470. unsigned long start = (unsigned long) addr;
  471. unsigned long end = (unsigned long) addr + length;
  472. /* For swizzling simply ensure that we always flush both
  473. * channels. Lame, but simple and it works. Swizzled
  474. * pwrite/pread is far from a hotpath - current userspace
  475. * doesn't use it at all. */
  476. start = round_down(start, 128);
  477. end = round_up(end, 128);
  478. drm_clflush_virt_range((void *)start, end - start);
  479. } else {
  480. drm_clflush_virt_range(addr, length);
  481. }
  482. }
  483. /* Only difference to the fast-path function is that this can handle bit17
  484. * and uses non-atomic copy and kmap functions. */
  485. static int
  486. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  487. char __user *user_data,
  488. bool page_do_bit17_swizzling, bool needs_clflush)
  489. {
  490. char *vaddr;
  491. int ret;
  492. vaddr = kmap(page);
  493. if (needs_clflush)
  494. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  495. page_length,
  496. page_do_bit17_swizzling);
  497. if (page_do_bit17_swizzling)
  498. ret = __copy_to_user_swizzled(user_data,
  499. vaddr, shmem_page_offset,
  500. page_length);
  501. else
  502. ret = __copy_to_user(user_data,
  503. vaddr + shmem_page_offset,
  504. page_length);
  505. kunmap(page);
  506. return ret ? - EFAULT : 0;
  507. }
  508. static int
  509. i915_gem_shmem_pread(struct drm_device *dev,
  510. struct drm_i915_gem_object *obj,
  511. struct drm_i915_gem_pread *args,
  512. struct drm_file *file)
  513. {
  514. char __user *user_data;
  515. ssize_t remain;
  516. loff_t offset;
  517. int shmem_page_offset, page_length, ret = 0;
  518. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  519. int prefaulted = 0;
  520. int needs_clflush = 0;
  521. struct sg_page_iter sg_iter;
  522. user_data = to_user_ptr(args->data_ptr);
  523. remain = args->size;
  524. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  525. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  526. if (ret)
  527. return ret;
  528. offset = args->offset;
  529. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  530. offset >> PAGE_SHIFT) {
  531. struct page *page = sg_page_iter_page(&sg_iter);
  532. if (remain <= 0)
  533. break;
  534. /* Operation in this page
  535. *
  536. * shmem_page_offset = offset within page in shmem file
  537. * page_length = bytes to copy for this page
  538. */
  539. shmem_page_offset = offset_in_page(offset);
  540. page_length = remain;
  541. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  542. page_length = PAGE_SIZE - shmem_page_offset;
  543. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  544. (page_to_phys(page) & (1 << 17)) != 0;
  545. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  546. user_data, page_do_bit17_swizzling,
  547. needs_clflush);
  548. if (ret == 0)
  549. goto next_page;
  550. mutex_unlock(&dev->struct_mutex);
  551. if (likely(!i915.prefault_disable) && !prefaulted) {
  552. ret = fault_in_multipages_writeable(user_data, remain);
  553. /* Userspace is tricking us, but we've already clobbered
  554. * its pages with the prefault and promised to write the
  555. * data up to the first fault. Hence ignore any errors
  556. * and just continue. */
  557. (void)ret;
  558. prefaulted = 1;
  559. }
  560. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  561. user_data, page_do_bit17_swizzling,
  562. needs_clflush);
  563. mutex_lock(&dev->struct_mutex);
  564. if (ret)
  565. goto out;
  566. next_page:
  567. remain -= page_length;
  568. user_data += page_length;
  569. offset += page_length;
  570. }
  571. out:
  572. i915_gem_object_unpin_pages(obj);
  573. return ret;
  574. }
  575. /**
  576. * Reads data from the object referenced by handle.
  577. *
  578. * On error, the contents of *data are undefined.
  579. */
  580. int
  581. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  582. struct drm_file *file)
  583. {
  584. struct drm_i915_gem_pread *args = data;
  585. struct drm_i915_gem_object *obj;
  586. int ret = 0;
  587. if (args->size == 0)
  588. return 0;
  589. if (!access_ok(VERIFY_WRITE,
  590. to_user_ptr(args->data_ptr),
  591. args->size))
  592. return -EFAULT;
  593. ret = i915_mutex_lock_interruptible(dev);
  594. if (ret)
  595. return ret;
  596. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  597. if (&obj->base == NULL) {
  598. ret = -ENOENT;
  599. goto unlock;
  600. }
  601. /* Bounds check source. */
  602. if (args->offset > obj->base.size ||
  603. args->size > obj->base.size - args->offset) {
  604. ret = -EINVAL;
  605. goto out;
  606. }
  607. /* prime objects have no backing filp to GEM pread/pwrite
  608. * pages from.
  609. */
  610. if (!obj->base.filp) {
  611. ret = -EINVAL;
  612. goto out;
  613. }
  614. trace_i915_gem_object_pread(obj, args->offset, args->size);
  615. ret = i915_gem_shmem_pread(dev, obj, args, file);
  616. out:
  617. drm_gem_object_unreference(&obj->base);
  618. unlock:
  619. mutex_unlock(&dev->struct_mutex);
  620. return ret;
  621. }
  622. /* This is the fast write path which cannot handle
  623. * page faults in the source data
  624. */
  625. static inline int
  626. fast_user_write(struct io_mapping *mapping,
  627. loff_t page_base, int page_offset,
  628. char __user *user_data,
  629. int length)
  630. {
  631. void __iomem *vaddr_atomic;
  632. void *vaddr;
  633. unsigned long unwritten;
  634. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  635. /* We can use the cpu mem copy function because this is X86. */
  636. vaddr = (void __force*)vaddr_atomic + page_offset;
  637. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  638. user_data, length);
  639. io_mapping_unmap_atomic(vaddr_atomic);
  640. return unwritten;
  641. }
  642. /**
  643. * This is the fast pwrite path, where we copy the data directly from the
  644. * user into the GTT, uncached.
  645. */
  646. static int
  647. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  648. struct drm_i915_gem_object *obj,
  649. struct drm_i915_gem_pwrite *args,
  650. struct drm_file *file)
  651. {
  652. struct drm_i915_private *dev_priv = dev->dev_private;
  653. ssize_t remain;
  654. loff_t offset, page_base;
  655. char __user *user_data;
  656. int page_offset, page_length, ret;
  657. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  658. if (ret)
  659. goto out;
  660. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  661. if (ret)
  662. goto out_unpin;
  663. ret = i915_gem_object_put_fence(obj);
  664. if (ret)
  665. goto out_unpin;
  666. user_data = to_user_ptr(args->data_ptr);
  667. remain = args->size;
  668. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  669. while (remain > 0) {
  670. /* Operation in this page
  671. *
  672. * page_base = page offset within aperture
  673. * page_offset = offset within page
  674. * page_length = bytes to copy for this page
  675. */
  676. page_base = offset & PAGE_MASK;
  677. page_offset = offset_in_page(offset);
  678. page_length = remain;
  679. if ((page_offset + remain) > PAGE_SIZE)
  680. page_length = PAGE_SIZE - page_offset;
  681. /* If we get a fault while copying data, then (presumably) our
  682. * source page isn't available. Return the error and we'll
  683. * retry in the slow path.
  684. */
  685. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  686. page_offset, user_data, page_length)) {
  687. ret = -EFAULT;
  688. goto out_unpin;
  689. }
  690. remain -= page_length;
  691. user_data += page_length;
  692. offset += page_length;
  693. }
  694. out_unpin:
  695. i915_gem_object_ggtt_unpin(obj);
  696. out:
  697. return ret;
  698. }
  699. /* Per-page copy function for the shmem pwrite fastpath.
  700. * Flushes invalid cachelines before writing to the target if
  701. * needs_clflush_before is set and flushes out any written cachelines after
  702. * writing if needs_clflush is set. */
  703. static int
  704. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  705. char __user *user_data,
  706. bool page_do_bit17_swizzling,
  707. bool needs_clflush_before,
  708. bool needs_clflush_after)
  709. {
  710. char *vaddr;
  711. int ret;
  712. if (unlikely(page_do_bit17_swizzling))
  713. return -EINVAL;
  714. vaddr = kmap_atomic(page);
  715. if (needs_clflush_before)
  716. drm_clflush_virt_range(vaddr + shmem_page_offset,
  717. page_length);
  718. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  719. user_data, page_length);
  720. if (needs_clflush_after)
  721. drm_clflush_virt_range(vaddr + shmem_page_offset,
  722. page_length);
  723. kunmap_atomic(vaddr);
  724. return ret ? -EFAULT : 0;
  725. }
  726. /* Only difference to the fast-path function is that this can handle bit17
  727. * and uses non-atomic copy and kmap functions. */
  728. static int
  729. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  730. char __user *user_data,
  731. bool page_do_bit17_swizzling,
  732. bool needs_clflush_before,
  733. bool needs_clflush_after)
  734. {
  735. char *vaddr;
  736. int ret;
  737. vaddr = kmap(page);
  738. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  739. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  740. page_length,
  741. page_do_bit17_swizzling);
  742. if (page_do_bit17_swizzling)
  743. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  744. user_data,
  745. page_length);
  746. else
  747. ret = __copy_from_user(vaddr + shmem_page_offset,
  748. user_data,
  749. page_length);
  750. if (needs_clflush_after)
  751. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  752. page_length,
  753. page_do_bit17_swizzling);
  754. kunmap(page);
  755. return ret ? -EFAULT : 0;
  756. }
  757. static int
  758. i915_gem_shmem_pwrite(struct drm_device *dev,
  759. struct drm_i915_gem_object *obj,
  760. struct drm_i915_gem_pwrite *args,
  761. struct drm_file *file)
  762. {
  763. ssize_t remain;
  764. loff_t offset;
  765. char __user *user_data;
  766. int shmem_page_offset, page_length, ret = 0;
  767. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  768. int hit_slowpath = 0;
  769. int needs_clflush_after = 0;
  770. int needs_clflush_before = 0;
  771. struct sg_page_iter sg_iter;
  772. user_data = to_user_ptr(args->data_ptr);
  773. remain = args->size;
  774. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  775. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  776. /* If we're not in the cpu write domain, set ourself into the gtt
  777. * write domain and manually flush cachelines (if required). This
  778. * optimizes for the case when the gpu will use the data
  779. * right away and we therefore have to clflush anyway. */
  780. needs_clflush_after = cpu_write_needs_clflush(obj);
  781. ret = i915_gem_object_wait_rendering(obj, false);
  782. if (ret)
  783. return ret;
  784. i915_gem_object_retire(obj);
  785. }
  786. /* Same trick applies to invalidate partially written cachelines read
  787. * before writing. */
  788. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  789. needs_clflush_before =
  790. !cpu_cache_is_coherent(dev, obj->cache_level);
  791. ret = i915_gem_object_get_pages(obj);
  792. if (ret)
  793. return ret;
  794. i915_gem_object_pin_pages(obj);
  795. offset = args->offset;
  796. obj->dirty = 1;
  797. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  798. offset >> PAGE_SHIFT) {
  799. struct page *page = sg_page_iter_page(&sg_iter);
  800. int partial_cacheline_write;
  801. if (remain <= 0)
  802. break;
  803. /* Operation in this page
  804. *
  805. * shmem_page_offset = offset within page in shmem file
  806. * page_length = bytes to copy for this page
  807. */
  808. shmem_page_offset = offset_in_page(offset);
  809. page_length = remain;
  810. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  811. page_length = PAGE_SIZE - shmem_page_offset;
  812. /* If we don't overwrite a cacheline completely we need to be
  813. * careful to have up-to-date data by first clflushing. Don't
  814. * overcomplicate things and flush the entire patch. */
  815. partial_cacheline_write = needs_clflush_before &&
  816. ((shmem_page_offset | page_length)
  817. & (boot_cpu_data.x86_clflush_size - 1));
  818. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  819. (page_to_phys(page) & (1 << 17)) != 0;
  820. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  821. user_data, page_do_bit17_swizzling,
  822. partial_cacheline_write,
  823. needs_clflush_after);
  824. if (ret == 0)
  825. goto next_page;
  826. hit_slowpath = 1;
  827. mutex_unlock(&dev->struct_mutex);
  828. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  829. user_data, page_do_bit17_swizzling,
  830. partial_cacheline_write,
  831. needs_clflush_after);
  832. mutex_lock(&dev->struct_mutex);
  833. if (ret)
  834. goto out;
  835. next_page:
  836. remain -= page_length;
  837. user_data += page_length;
  838. offset += page_length;
  839. }
  840. out:
  841. i915_gem_object_unpin_pages(obj);
  842. if (hit_slowpath) {
  843. /*
  844. * Fixup: Flush cpu caches in case we didn't flush the dirty
  845. * cachelines in-line while writing and the object moved
  846. * out of the cpu write domain while we've dropped the lock.
  847. */
  848. if (!needs_clflush_after &&
  849. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  850. if (i915_gem_clflush_object(obj, obj->pin_display))
  851. i915_gem_chipset_flush(dev);
  852. }
  853. }
  854. if (needs_clflush_after)
  855. i915_gem_chipset_flush(dev);
  856. return ret;
  857. }
  858. /**
  859. * Writes data to the object referenced by handle.
  860. *
  861. * On error, the contents of the buffer that were to be modified are undefined.
  862. */
  863. int
  864. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  865. struct drm_file *file)
  866. {
  867. struct drm_i915_private *dev_priv = dev->dev_private;
  868. struct drm_i915_gem_pwrite *args = data;
  869. struct drm_i915_gem_object *obj;
  870. int ret;
  871. if (args->size == 0)
  872. return 0;
  873. if (!access_ok(VERIFY_READ,
  874. to_user_ptr(args->data_ptr),
  875. args->size))
  876. return -EFAULT;
  877. if (likely(!i915.prefault_disable)) {
  878. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  879. args->size);
  880. if (ret)
  881. return -EFAULT;
  882. }
  883. intel_runtime_pm_get(dev_priv);
  884. ret = i915_mutex_lock_interruptible(dev);
  885. if (ret)
  886. goto put_rpm;
  887. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  888. if (&obj->base == NULL) {
  889. ret = -ENOENT;
  890. goto unlock;
  891. }
  892. /* Bounds check destination. */
  893. if (args->offset > obj->base.size ||
  894. args->size > obj->base.size - args->offset) {
  895. ret = -EINVAL;
  896. goto out;
  897. }
  898. /* prime objects have no backing filp to GEM pread/pwrite
  899. * pages from.
  900. */
  901. if (!obj->base.filp) {
  902. ret = -EINVAL;
  903. goto out;
  904. }
  905. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  906. ret = -EFAULT;
  907. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  908. * it would end up going through the fenced access, and we'll get
  909. * different detiling behavior between reading and writing.
  910. * pread/pwrite currently are reading and writing from the CPU
  911. * perspective, requiring manual detiling by the client.
  912. */
  913. if (obj->tiling_mode == I915_TILING_NONE &&
  914. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  915. cpu_write_needs_clflush(obj)) {
  916. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  917. /* Note that the gtt paths might fail with non-page-backed user
  918. * pointers (e.g. gtt mappings when moving data between
  919. * textures). Fallback to the shmem path in that case. */
  920. }
  921. if (ret == -EFAULT || ret == -ENOSPC) {
  922. if (obj->phys_handle)
  923. ret = i915_gem_phys_pwrite(obj, args, file);
  924. else
  925. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  926. }
  927. out:
  928. drm_gem_object_unreference(&obj->base);
  929. unlock:
  930. mutex_unlock(&dev->struct_mutex);
  931. put_rpm:
  932. intel_runtime_pm_put(dev_priv);
  933. return ret;
  934. }
  935. int
  936. i915_gem_check_wedge(struct i915_gpu_error *error,
  937. bool interruptible)
  938. {
  939. if (i915_reset_in_progress(error)) {
  940. /* Non-interruptible callers can't handle -EAGAIN, hence return
  941. * -EIO unconditionally for these. */
  942. if (!interruptible)
  943. return -EIO;
  944. /* Recovery complete, but the reset failed ... */
  945. if (i915_terminally_wedged(error))
  946. return -EIO;
  947. /*
  948. * Check if GPU Reset is in progress - we need intel_ring_begin
  949. * to work properly to reinit the hw state while the gpu is
  950. * still marked as reset-in-progress. Handle this with a flag.
  951. */
  952. if (!error->reload_in_reset)
  953. return -EAGAIN;
  954. }
  955. return 0;
  956. }
  957. /*
  958. * Compare arbitrary request against outstanding lazy request. Emit on match.
  959. */
  960. int
  961. i915_gem_check_olr(struct drm_i915_gem_request *req)
  962. {
  963. int ret;
  964. WARN_ON(!mutex_is_locked(&req->ring->dev->struct_mutex));
  965. ret = 0;
  966. if (req == req->ring->outstanding_lazy_request)
  967. ret = i915_add_request(req->ring);
  968. return ret;
  969. }
  970. static void fake_irq(unsigned long data)
  971. {
  972. wake_up_process((struct task_struct *)data);
  973. }
  974. static bool missed_irq(struct drm_i915_private *dev_priv,
  975. struct intel_engine_cs *ring)
  976. {
  977. return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  978. }
  979. static bool can_wait_boost(struct drm_i915_file_private *file_priv)
  980. {
  981. if (file_priv == NULL)
  982. return true;
  983. return !atomic_xchg(&file_priv->rps_wait_boost, true);
  984. }
  985. /**
  986. * __i915_wait_request - wait until execution of request has finished
  987. * @req: duh!
  988. * @reset_counter: reset sequence associated with the given request
  989. * @interruptible: do an interruptible wait (normally yes)
  990. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  991. *
  992. * Note: It is of utmost importance that the passed in seqno and reset_counter
  993. * values have been read by the caller in an smp safe manner. Where read-side
  994. * locks are involved, it is sufficient to read the reset_counter before
  995. * unlocking the lock that protects the seqno. For lockless tricks, the
  996. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  997. * inserted.
  998. *
  999. * Returns 0 if the request was found within the alloted time. Else returns the
  1000. * errno with remaining time filled in timeout argument.
  1001. */
  1002. int __i915_wait_request(struct drm_i915_gem_request *req,
  1003. unsigned reset_counter,
  1004. bool interruptible,
  1005. s64 *timeout,
  1006. struct drm_i915_file_private *file_priv)
  1007. {
  1008. struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  1009. struct drm_device *dev = ring->dev;
  1010. struct drm_i915_private *dev_priv = dev->dev_private;
  1011. const bool irq_test_in_progress =
  1012. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  1013. DEFINE_WAIT(wait);
  1014. unsigned long timeout_expire;
  1015. s64 before, now;
  1016. int ret;
  1017. WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1018. if (i915_gem_request_completed(req, true))
  1019. return 0;
  1020. timeout_expire = timeout ?
  1021. jiffies + nsecs_to_jiffies_timeout((u64)*timeout) : 0;
  1022. if (INTEL_INFO(dev)->gen >= 6 && ring->id == RCS && can_wait_boost(file_priv)) {
  1023. gen6_rps_boost(dev_priv);
  1024. if (file_priv)
  1025. mod_delayed_work(dev_priv->wq,
  1026. &file_priv->mm.idle_work,
  1027. msecs_to_jiffies(100));
  1028. }
  1029. if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring)))
  1030. return -ENODEV;
  1031. /* Record current time in case interrupted by signal, or wedged */
  1032. trace_i915_gem_request_wait_begin(req);
  1033. before = ktime_get_raw_ns();
  1034. for (;;) {
  1035. struct timer_list timer;
  1036. prepare_to_wait(&ring->irq_queue, &wait,
  1037. interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  1038. /* We need to check whether any gpu reset happened in between
  1039. * the caller grabbing the seqno and now ... */
  1040. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  1041. /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  1042. * is truely gone. */
  1043. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1044. if (ret == 0)
  1045. ret = -EAGAIN;
  1046. break;
  1047. }
  1048. if (i915_gem_request_completed(req, false)) {
  1049. ret = 0;
  1050. break;
  1051. }
  1052. if (interruptible && signal_pending(current)) {
  1053. ret = -ERESTARTSYS;
  1054. break;
  1055. }
  1056. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1057. ret = -ETIME;
  1058. break;
  1059. }
  1060. timer.function = NULL;
  1061. if (timeout || missed_irq(dev_priv, ring)) {
  1062. unsigned long expire;
  1063. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1064. expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
  1065. mod_timer(&timer, expire);
  1066. }
  1067. io_schedule();
  1068. if (timer.function) {
  1069. del_singleshot_timer_sync(&timer);
  1070. destroy_timer_on_stack(&timer);
  1071. }
  1072. }
  1073. now = ktime_get_raw_ns();
  1074. trace_i915_gem_request_wait_end(req);
  1075. if (!irq_test_in_progress)
  1076. ring->irq_put(ring);
  1077. finish_wait(&ring->irq_queue, &wait);
  1078. if (timeout) {
  1079. s64 tres = *timeout - (now - before);
  1080. *timeout = tres < 0 ? 0 : tres;
  1081. /*
  1082. * Apparently ktime isn't accurate enough and occasionally has a
  1083. * bit of mismatch in the jiffies<->nsecs<->ktime loop. So patch
  1084. * things up to make the test happy. We allow up to 1 jiffy.
  1085. *
  1086. * This is a regrssion from the timespec->ktime conversion.
  1087. */
  1088. if (ret == -ETIME && *timeout < jiffies_to_usecs(1)*1000)
  1089. *timeout = 0;
  1090. }
  1091. return ret;
  1092. }
  1093. /**
  1094. * Waits for a request to be signaled, and cleans up the
  1095. * request and object lists appropriately for that event.
  1096. */
  1097. int
  1098. i915_wait_request(struct drm_i915_gem_request *req)
  1099. {
  1100. struct drm_device *dev;
  1101. struct drm_i915_private *dev_priv;
  1102. bool interruptible;
  1103. unsigned reset_counter;
  1104. int ret;
  1105. BUG_ON(req == NULL);
  1106. dev = req->ring->dev;
  1107. dev_priv = dev->dev_private;
  1108. interruptible = dev_priv->mm.interruptible;
  1109. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1110. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1111. if (ret)
  1112. return ret;
  1113. ret = i915_gem_check_olr(req);
  1114. if (ret)
  1115. return ret;
  1116. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1117. i915_gem_request_reference(req);
  1118. ret = __i915_wait_request(req, reset_counter,
  1119. interruptible, NULL, NULL);
  1120. i915_gem_request_unreference(req);
  1121. return ret;
  1122. }
  1123. static int
  1124. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj)
  1125. {
  1126. if (!obj->active)
  1127. return 0;
  1128. /* Manually manage the write flush as we may have not yet
  1129. * retired the buffer.
  1130. *
  1131. * Note that the last_write_req is always the earlier of
  1132. * the two (read/write) requests, so if we haved successfully waited,
  1133. * we know we have passed the last write.
  1134. */
  1135. i915_gem_request_assign(&obj->last_write_req, NULL);
  1136. return 0;
  1137. }
  1138. /**
  1139. * Ensures that all rendering to the object has completed and the object is
  1140. * safe to unbind from the GTT or access from the CPU.
  1141. */
  1142. static __must_check int
  1143. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1144. bool readonly)
  1145. {
  1146. struct drm_i915_gem_request *req;
  1147. int ret;
  1148. req = readonly ? obj->last_write_req : obj->last_read_req;
  1149. if (!req)
  1150. return 0;
  1151. ret = i915_wait_request(req);
  1152. if (ret)
  1153. return ret;
  1154. return i915_gem_object_wait_rendering__tail(obj);
  1155. }
  1156. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1157. * as the object state may change during this call.
  1158. */
  1159. static __must_check int
  1160. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1161. struct drm_i915_file_private *file_priv,
  1162. bool readonly)
  1163. {
  1164. struct drm_i915_gem_request *req;
  1165. struct drm_device *dev = obj->base.dev;
  1166. struct drm_i915_private *dev_priv = dev->dev_private;
  1167. unsigned reset_counter;
  1168. int ret;
  1169. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1170. BUG_ON(!dev_priv->mm.interruptible);
  1171. req = readonly ? obj->last_write_req : obj->last_read_req;
  1172. if (!req)
  1173. return 0;
  1174. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1175. if (ret)
  1176. return ret;
  1177. ret = i915_gem_check_olr(req);
  1178. if (ret)
  1179. return ret;
  1180. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1181. i915_gem_request_reference(req);
  1182. mutex_unlock(&dev->struct_mutex);
  1183. ret = __i915_wait_request(req, reset_counter, true, NULL, file_priv);
  1184. mutex_lock(&dev->struct_mutex);
  1185. i915_gem_request_unreference(req);
  1186. if (ret)
  1187. return ret;
  1188. return i915_gem_object_wait_rendering__tail(obj);
  1189. }
  1190. /**
  1191. * Called when user space prepares to use an object with the CPU, either
  1192. * through the mmap ioctl's mapping or a GTT mapping.
  1193. */
  1194. int
  1195. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1196. struct drm_file *file)
  1197. {
  1198. struct drm_i915_gem_set_domain *args = data;
  1199. struct drm_i915_gem_object *obj;
  1200. uint32_t read_domains = args->read_domains;
  1201. uint32_t write_domain = args->write_domain;
  1202. int ret;
  1203. /* Only handle setting domains to types used by the CPU. */
  1204. if (write_domain & I915_GEM_GPU_DOMAINS)
  1205. return -EINVAL;
  1206. if (read_domains & I915_GEM_GPU_DOMAINS)
  1207. return -EINVAL;
  1208. /* Having something in the write domain implies it's in the read
  1209. * domain, and only that read domain. Enforce that in the request.
  1210. */
  1211. if (write_domain != 0 && read_domains != write_domain)
  1212. return -EINVAL;
  1213. ret = i915_mutex_lock_interruptible(dev);
  1214. if (ret)
  1215. return ret;
  1216. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1217. if (&obj->base == NULL) {
  1218. ret = -ENOENT;
  1219. goto unlock;
  1220. }
  1221. /* Try to flush the object off the GPU without holding the lock.
  1222. * We will repeat the flush holding the lock in the normal manner
  1223. * to catch cases where we are gazumped.
  1224. */
  1225. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1226. file->driver_priv,
  1227. !write_domain);
  1228. if (ret)
  1229. goto unref;
  1230. if (read_domains & I915_GEM_DOMAIN_GTT)
  1231. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1232. else
  1233. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1234. unref:
  1235. drm_gem_object_unreference(&obj->base);
  1236. unlock:
  1237. mutex_unlock(&dev->struct_mutex);
  1238. return ret;
  1239. }
  1240. /**
  1241. * Called when user space has done writes to this buffer
  1242. */
  1243. int
  1244. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1245. struct drm_file *file)
  1246. {
  1247. struct drm_i915_gem_sw_finish *args = data;
  1248. struct drm_i915_gem_object *obj;
  1249. int ret = 0;
  1250. ret = i915_mutex_lock_interruptible(dev);
  1251. if (ret)
  1252. return ret;
  1253. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1254. if (&obj->base == NULL) {
  1255. ret = -ENOENT;
  1256. goto unlock;
  1257. }
  1258. /* Pinned buffers may be scanout, so flush the cache */
  1259. if (obj->pin_display)
  1260. i915_gem_object_flush_cpu_write_domain(obj, true);
  1261. drm_gem_object_unreference(&obj->base);
  1262. unlock:
  1263. mutex_unlock(&dev->struct_mutex);
  1264. return ret;
  1265. }
  1266. /**
  1267. * Maps the contents of an object, returning the address it is mapped
  1268. * into.
  1269. *
  1270. * While the mapping holds a reference on the contents of the object, it doesn't
  1271. * imply a ref on the object itself.
  1272. *
  1273. * IMPORTANT:
  1274. *
  1275. * DRM driver writers who look a this function as an example for how to do GEM
  1276. * mmap support, please don't implement mmap support like here. The modern way
  1277. * to implement DRM mmap support is with an mmap offset ioctl (like
  1278. * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1279. * That way debug tooling like valgrind will understand what's going on, hiding
  1280. * the mmap call in a driver private ioctl will break that. The i915 driver only
  1281. * does cpu mmaps this way because we didn't know better.
  1282. */
  1283. int
  1284. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1285. struct drm_file *file)
  1286. {
  1287. struct drm_i915_gem_mmap *args = data;
  1288. struct drm_gem_object *obj;
  1289. unsigned long addr;
  1290. if (args->flags & ~(I915_MMAP_WC))
  1291. return -EINVAL;
  1292. if (args->flags & I915_MMAP_WC && !cpu_has_pat)
  1293. return -ENODEV;
  1294. obj = drm_gem_object_lookup(dev, file, args->handle);
  1295. if (obj == NULL)
  1296. return -ENOENT;
  1297. /* prime objects have no backing filp to GEM mmap
  1298. * pages from.
  1299. */
  1300. if (!obj->filp) {
  1301. drm_gem_object_unreference_unlocked(obj);
  1302. return -EINVAL;
  1303. }
  1304. addr = vm_mmap(obj->filp, 0, args->size,
  1305. PROT_READ | PROT_WRITE, MAP_SHARED,
  1306. args->offset);
  1307. if (args->flags & I915_MMAP_WC) {
  1308. struct mm_struct *mm = current->mm;
  1309. struct vm_area_struct *vma;
  1310. down_write(&mm->mmap_sem);
  1311. vma = find_vma(mm, addr);
  1312. if (vma)
  1313. vma->vm_page_prot =
  1314. pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
  1315. else
  1316. addr = -ENOMEM;
  1317. up_write(&mm->mmap_sem);
  1318. }
  1319. drm_gem_object_unreference_unlocked(obj);
  1320. if (IS_ERR((void *)addr))
  1321. return addr;
  1322. args->addr_ptr = (uint64_t) addr;
  1323. return 0;
  1324. }
  1325. /**
  1326. * i915_gem_fault - fault a page into the GTT
  1327. * vma: VMA in question
  1328. * vmf: fault info
  1329. *
  1330. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1331. * from userspace. The fault handler takes care of binding the object to
  1332. * the GTT (if needed), allocating and programming a fence register (again,
  1333. * only if needed based on whether the old reg is still valid or the object
  1334. * is tiled) and inserting a new PTE into the faulting process.
  1335. *
  1336. * Note that the faulting process may involve evicting existing objects
  1337. * from the GTT and/or fence registers to make room. So performance may
  1338. * suffer if the GTT working set is large or there are few fence registers
  1339. * left.
  1340. */
  1341. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1342. {
  1343. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1344. struct drm_device *dev = obj->base.dev;
  1345. struct drm_i915_private *dev_priv = dev->dev_private;
  1346. pgoff_t page_offset;
  1347. unsigned long pfn;
  1348. int ret = 0;
  1349. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1350. intel_runtime_pm_get(dev_priv);
  1351. /* We don't use vmf->pgoff since that has the fake offset */
  1352. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1353. PAGE_SHIFT;
  1354. ret = i915_mutex_lock_interruptible(dev);
  1355. if (ret)
  1356. goto out;
  1357. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1358. /* Try to flush the object off the GPU first without holding the lock.
  1359. * Upon reacquiring the lock, we will perform our sanity checks and then
  1360. * repeat the flush holding the lock in the normal manner to catch cases
  1361. * where we are gazumped.
  1362. */
  1363. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1364. if (ret)
  1365. goto unlock;
  1366. /* Access to snoopable pages through the GTT is incoherent. */
  1367. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1368. ret = -EFAULT;
  1369. goto unlock;
  1370. }
  1371. /* Now bind it into the GTT if needed */
  1372. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
  1373. if (ret)
  1374. goto unlock;
  1375. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1376. if (ret)
  1377. goto unpin;
  1378. ret = i915_gem_object_get_fence(obj);
  1379. if (ret)
  1380. goto unpin;
  1381. /* Finally, remap it using the new GTT offset */
  1382. pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1383. pfn >>= PAGE_SHIFT;
  1384. if (!obj->fault_mappable) {
  1385. unsigned long size = min_t(unsigned long,
  1386. vma->vm_end - vma->vm_start,
  1387. obj->base.size);
  1388. int i;
  1389. for (i = 0; i < size >> PAGE_SHIFT; i++) {
  1390. ret = vm_insert_pfn(vma,
  1391. (unsigned long)vma->vm_start + i * PAGE_SIZE,
  1392. pfn + i);
  1393. if (ret)
  1394. break;
  1395. }
  1396. obj->fault_mappable = true;
  1397. } else
  1398. ret = vm_insert_pfn(vma,
  1399. (unsigned long)vmf->virtual_address,
  1400. pfn + page_offset);
  1401. unpin:
  1402. i915_gem_object_ggtt_unpin(obj);
  1403. unlock:
  1404. mutex_unlock(&dev->struct_mutex);
  1405. out:
  1406. switch (ret) {
  1407. case -EIO:
  1408. /*
  1409. * We eat errors when the gpu is terminally wedged to avoid
  1410. * userspace unduly crashing (gl has no provisions for mmaps to
  1411. * fail). But any other -EIO isn't ours (e.g. swap in failure)
  1412. * and so needs to be reported.
  1413. */
  1414. if (!i915_terminally_wedged(&dev_priv->gpu_error)) {
  1415. ret = VM_FAULT_SIGBUS;
  1416. break;
  1417. }
  1418. case -EAGAIN:
  1419. /*
  1420. * EAGAIN means the gpu is hung and we'll wait for the error
  1421. * handler to reset everything when re-faulting in
  1422. * i915_mutex_lock_interruptible.
  1423. */
  1424. case 0:
  1425. case -ERESTARTSYS:
  1426. case -EINTR:
  1427. case -EBUSY:
  1428. /*
  1429. * EBUSY is ok: this just means that another thread
  1430. * already did the job.
  1431. */
  1432. ret = VM_FAULT_NOPAGE;
  1433. break;
  1434. case -ENOMEM:
  1435. ret = VM_FAULT_OOM;
  1436. break;
  1437. case -ENOSPC:
  1438. case -EFAULT:
  1439. ret = VM_FAULT_SIGBUS;
  1440. break;
  1441. default:
  1442. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1443. ret = VM_FAULT_SIGBUS;
  1444. break;
  1445. }
  1446. intel_runtime_pm_put(dev_priv);
  1447. return ret;
  1448. }
  1449. /**
  1450. * i915_gem_release_mmap - remove physical page mappings
  1451. * @obj: obj in question
  1452. *
  1453. * Preserve the reservation of the mmapping with the DRM core code, but
  1454. * relinquish ownership of the pages back to the system.
  1455. *
  1456. * It is vital that we remove the page mapping if we have mapped a tiled
  1457. * object through the GTT and then lose the fence register due to
  1458. * resource pressure. Similarly if the object has been moved out of the
  1459. * aperture, than pages mapped into userspace must be revoked. Removing the
  1460. * mapping will then trigger a page fault on the next user access, allowing
  1461. * fixup by i915_gem_fault().
  1462. */
  1463. void
  1464. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1465. {
  1466. if (!obj->fault_mappable)
  1467. return;
  1468. drm_vma_node_unmap(&obj->base.vma_node,
  1469. obj->base.dev->anon_inode->i_mapping);
  1470. obj->fault_mappable = false;
  1471. }
  1472. void
  1473. i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1474. {
  1475. struct drm_i915_gem_object *obj;
  1476. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  1477. i915_gem_release_mmap(obj);
  1478. }
  1479. uint32_t
  1480. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1481. {
  1482. uint32_t gtt_size;
  1483. if (INTEL_INFO(dev)->gen >= 4 ||
  1484. tiling_mode == I915_TILING_NONE)
  1485. return size;
  1486. /* Previous chips need a power-of-two fence region when tiling */
  1487. if (INTEL_INFO(dev)->gen == 3)
  1488. gtt_size = 1024*1024;
  1489. else
  1490. gtt_size = 512*1024;
  1491. while (gtt_size < size)
  1492. gtt_size <<= 1;
  1493. return gtt_size;
  1494. }
  1495. /**
  1496. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1497. * @obj: object to check
  1498. *
  1499. * Return the required GTT alignment for an object, taking into account
  1500. * potential fence register mapping.
  1501. */
  1502. uint32_t
  1503. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1504. int tiling_mode, bool fenced)
  1505. {
  1506. /*
  1507. * Minimum alignment is 4k (GTT page size), but might be greater
  1508. * if a fence register is needed for the object.
  1509. */
  1510. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1511. tiling_mode == I915_TILING_NONE)
  1512. return 4096;
  1513. /*
  1514. * Previous chips need to be aligned to the size of the smallest
  1515. * fence register that can contain the object.
  1516. */
  1517. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1518. }
  1519. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1520. {
  1521. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1522. int ret;
  1523. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1524. return 0;
  1525. dev_priv->mm.shrinker_no_lock_stealing = true;
  1526. ret = drm_gem_create_mmap_offset(&obj->base);
  1527. if (ret != -ENOSPC)
  1528. goto out;
  1529. /* Badly fragmented mmap space? The only way we can recover
  1530. * space is by destroying unwanted objects. We can't randomly release
  1531. * mmap_offsets as userspace expects them to be persistent for the
  1532. * lifetime of the objects. The closest we can is to release the
  1533. * offsets on purgeable objects by truncating it and marking it purged,
  1534. * which prevents userspace from ever using that object again.
  1535. */
  1536. i915_gem_shrink(dev_priv,
  1537. obj->base.size >> PAGE_SHIFT,
  1538. I915_SHRINK_BOUND |
  1539. I915_SHRINK_UNBOUND |
  1540. I915_SHRINK_PURGEABLE);
  1541. ret = drm_gem_create_mmap_offset(&obj->base);
  1542. if (ret != -ENOSPC)
  1543. goto out;
  1544. i915_gem_shrink_all(dev_priv);
  1545. ret = drm_gem_create_mmap_offset(&obj->base);
  1546. out:
  1547. dev_priv->mm.shrinker_no_lock_stealing = false;
  1548. return ret;
  1549. }
  1550. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1551. {
  1552. drm_gem_free_mmap_offset(&obj->base);
  1553. }
  1554. int
  1555. i915_gem_mmap_gtt(struct drm_file *file,
  1556. struct drm_device *dev,
  1557. uint32_t handle,
  1558. uint64_t *offset)
  1559. {
  1560. struct drm_i915_private *dev_priv = dev->dev_private;
  1561. struct drm_i915_gem_object *obj;
  1562. int ret;
  1563. ret = i915_mutex_lock_interruptible(dev);
  1564. if (ret)
  1565. return ret;
  1566. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1567. if (&obj->base == NULL) {
  1568. ret = -ENOENT;
  1569. goto unlock;
  1570. }
  1571. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1572. ret = -E2BIG;
  1573. goto out;
  1574. }
  1575. if (obj->madv != I915_MADV_WILLNEED) {
  1576. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1577. ret = -EFAULT;
  1578. goto out;
  1579. }
  1580. ret = i915_gem_object_create_mmap_offset(obj);
  1581. if (ret)
  1582. goto out;
  1583. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1584. out:
  1585. drm_gem_object_unreference(&obj->base);
  1586. unlock:
  1587. mutex_unlock(&dev->struct_mutex);
  1588. return ret;
  1589. }
  1590. /**
  1591. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1592. * @dev: DRM device
  1593. * @data: GTT mapping ioctl data
  1594. * @file: GEM object info
  1595. *
  1596. * Simply returns the fake offset to userspace so it can mmap it.
  1597. * The mmap call will end up in drm_gem_mmap(), which will set things
  1598. * up so we can get faults in the handler above.
  1599. *
  1600. * The fault handler will take care of binding the object into the GTT
  1601. * (since it may have been evicted to make room for something), allocating
  1602. * a fence register, and mapping the appropriate aperture address into
  1603. * userspace.
  1604. */
  1605. int
  1606. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1607. struct drm_file *file)
  1608. {
  1609. struct drm_i915_gem_mmap_gtt *args = data;
  1610. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1611. }
  1612. static inline int
  1613. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1614. {
  1615. return obj->madv == I915_MADV_DONTNEED;
  1616. }
  1617. /* Immediately discard the backing storage */
  1618. static void
  1619. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1620. {
  1621. i915_gem_object_free_mmap_offset(obj);
  1622. if (obj->base.filp == NULL)
  1623. return;
  1624. /* Our goal here is to return as much of the memory as
  1625. * is possible back to the system as we are called from OOM.
  1626. * To do this we must instruct the shmfs to drop all of its
  1627. * backing pages, *now*.
  1628. */
  1629. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1630. obj->madv = __I915_MADV_PURGED;
  1631. }
  1632. /* Try to discard unwanted pages */
  1633. static void
  1634. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1635. {
  1636. struct address_space *mapping;
  1637. switch (obj->madv) {
  1638. case I915_MADV_DONTNEED:
  1639. i915_gem_object_truncate(obj);
  1640. case __I915_MADV_PURGED:
  1641. return;
  1642. }
  1643. if (obj->base.filp == NULL)
  1644. return;
  1645. mapping = file_inode(obj->base.filp)->i_mapping,
  1646. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1647. }
  1648. static void
  1649. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1650. {
  1651. struct sg_page_iter sg_iter;
  1652. int ret;
  1653. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1654. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1655. if (ret) {
  1656. /* In the event of a disaster, abandon all caches and
  1657. * hope for the best.
  1658. */
  1659. WARN_ON(ret != -EIO);
  1660. i915_gem_clflush_object(obj, true);
  1661. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1662. }
  1663. if (i915_gem_object_needs_bit17_swizzle(obj))
  1664. i915_gem_object_save_bit_17_swizzle(obj);
  1665. if (obj->madv == I915_MADV_DONTNEED)
  1666. obj->dirty = 0;
  1667. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1668. struct page *page = sg_page_iter_page(&sg_iter);
  1669. if (obj->dirty)
  1670. set_page_dirty(page);
  1671. if (obj->madv == I915_MADV_WILLNEED)
  1672. mark_page_accessed(page);
  1673. page_cache_release(page);
  1674. }
  1675. obj->dirty = 0;
  1676. sg_free_table(obj->pages);
  1677. kfree(obj->pages);
  1678. }
  1679. int
  1680. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1681. {
  1682. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1683. if (obj->pages == NULL)
  1684. return 0;
  1685. if (obj->pages_pin_count)
  1686. return -EBUSY;
  1687. BUG_ON(i915_gem_obj_bound_any(obj));
  1688. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1689. * array, hence protect them from being reaped by removing them from gtt
  1690. * lists early. */
  1691. list_del(&obj->global_list);
  1692. ops->put_pages(obj);
  1693. obj->pages = NULL;
  1694. i915_gem_object_invalidate(obj);
  1695. return 0;
  1696. }
  1697. unsigned long
  1698. i915_gem_shrink(struct drm_i915_private *dev_priv,
  1699. long target, unsigned flags)
  1700. {
  1701. const struct {
  1702. struct list_head *list;
  1703. unsigned int bit;
  1704. } phases[] = {
  1705. { &dev_priv->mm.unbound_list, I915_SHRINK_UNBOUND },
  1706. { &dev_priv->mm.bound_list, I915_SHRINK_BOUND },
  1707. { NULL, 0 },
  1708. }, *phase;
  1709. unsigned long count = 0;
  1710. /*
  1711. * As we may completely rewrite the (un)bound list whilst unbinding
  1712. * (due to retiring requests) we have to strictly process only
  1713. * one element of the list at the time, and recheck the list
  1714. * on every iteration.
  1715. *
  1716. * In particular, we must hold a reference whilst removing the
  1717. * object as we may end up waiting for and/or retiring the objects.
  1718. * This might release the final reference (held by the active list)
  1719. * and result in the object being freed from under us. This is
  1720. * similar to the precautions the eviction code must take whilst
  1721. * removing objects.
  1722. *
  1723. * Also note that although these lists do not hold a reference to
  1724. * the object we can safely grab one here: The final object
  1725. * unreferencing and the bound_list are both protected by the
  1726. * dev->struct_mutex and so we won't ever be able to observe an
  1727. * object on the bound_list with a reference count equals 0.
  1728. */
  1729. for (phase = phases; phase->list; phase++) {
  1730. struct list_head still_in_list;
  1731. if ((flags & phase->bit) == 0)
  1732. continue;
  1733. INIT_LIST_HEAD(&still_in_list);
  1734. while (count < target && !list_empty(phase->list)) {
  1735. struct drm_i915_gem_object *obj;
  1736. struct i915_vma *vma, *v;
  1737. obj = list_first_entry(phase->list,
  1738. typeof(*obj), global_list);
  1739. list_move_tail(&obj->global_list, &still_in_list);
  1740. if (flags & I915_SHRINK_PURGEABLE &&
  1741. !i915_gem_object_is_purgeable(obj))
  1742. continue;
  1743. drm_gem_object_reference(&obj->base);
  1744. /* For the unbound phase, this should be a no-op! */
  1745. list_for_each_entry_safe(vma, v,
  1746. &obj->vma_list, vma_link)
  1747. if (i915_vma_unbind(vma))
  1748. break;
  1749. if (i915_gem_object_put_pages(obj) == 0)
  1750. count += obj->base.size >> PAGE_SHIFT;
  1751. drm_gem_object_unreference(&obj->base);
  1752. }
  1753. list_splice(&still_in_list, phase->list);
  1754. }
  1755. return count;
  1756. }
  1757. static unsigned long
  1758. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1759. {
  1760. i915_gem_evict_everything(dev_priv->dev);
  1761. return i915_gem_shrink(dev_priv, LONG_MAX,
  1762. I915_SHRINK_BOUND | I915_SHRINK_UNBOUND);
  1763. }
  1764. static int
  1765. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1766. {
  1767. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1768. int page_count, i;
  1769. struct address_space *mapping;
  1770. struct sg_table *st;
  1771. struct scatterlist *sg;
  1772. struct sg_page_iter sg_iter;
  1773. struct page *page;
  1774. unsigned long last_pfn = 0; /* suppress gcc warning */
  1775. gfp_t gfp;
  1776. /* Assert that the object is not currently in any GPU domain. As it
  1777. * wasn't in the GTT, there shouldn't be any way it could have been in
  1778. * a GPU cache
  1779. */
  1780. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1781. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1782. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1783. if (st == NULL)
  1784. return -ENOMEM;
  1785. page_count = obj->base.size / PAGE_SIZE;
  1786. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1787. kfree(st);
  1788. return -ENOMEM;
  1789. }
  1790. /* Get the list of pages out of our struct file. They'll be pinned
  1791. * at this point until we release them.
  1792. *
  1793. * Fail silently without starting the shrinker
  1794. */
  1795. mapping = file_inode(obj->base.filp)->i_mapping;
  1796. gfp = mapping_gfp_mask(mapping);
  1797. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1798. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1799. sg = st->sgl;
  1800. st->nents = 0;
  1801. for (i = 0; i < page_count; i++) {
  1802. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1803. if (IS_ERR(page)) {
  1804. i915_gem_shrink(dev_priv,
  1805. page_count,
  1806. I915_SHRINK_BOUND |
  1807. I915_SHRINK_UNBOUND |
  1808. I915_SHRINK_PURGEABLE);
  1809. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1810. }
  1811. if (IS_ERR(page)) {
  1812. /* We've tried hard to allocate the memory by reaping
  1813. * our own buffer, now let the real VM do its job and
  1814. * go down in flames if truly OOM.
  1815. */
  1816. i915_gem_shrink_all(dev_priv);
  1817. page = shmem_read_mapping_page(mapping, i);
  1818. if (IS_ERR(page))
  1819. goto err_pages;
  1820. }
  1821. #ifdef CONFIG_SWIOTLB
  1822. if (swiotlb_nr_tbl()) {
  1823. st->nents++;
  1824. sg_set_page(sg, page, PAGE_SIZE, 0);
  1825. sg = sg_next(sg);
  1826. continue;
  1827. }
  1828. #endif
  1829. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1830. if (i)
  1831. sg = sg_next(sg);
  1832. st->nents++;
  1833. sg_set_page(sg, page, PAGE_SIZE, 0);
  1834. } else {
  1835. sg->length += PAGE_SIZE;
  1836. }
  1837. last_pfn = page_to_pfn(page);
  1838. /* Check that the i965g/gm workaround works. */
  1839. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1840. }
  1841. #ifdef CONFIG_SWIOTLB
  1842. if (!swiotlb_nr_tbl())
  1843. #endif
  1844. sg_mark_end(sg);
  1845. obj->pages = st;
  1846. if (i915_gem_object_needs_bit17_swizzle(obj))
  1847. i915_gem_object_do_bit_17_swizzle(obj);
  1848. if (obj->tiling_mode != I915_TILING_NONE &&
  1849. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1850. i915_gem_object_pin_pages(obj);
  1851. return 0;
  1852. err_pages:
  1853. sg_mark_end(sg);
  1854. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1855. page_cache_release(sg_page_iter_page(&sg_iter));
  1856. sg_free_table(st);
  1857. kfree(st);
  1858. /* shmemfs first checks if there is enough memory to allocate the page
  1859. * and reports ENOSPC should there be insufficient, along with the usual
  1860. * ENOMEM for a genuine allocation failure.
  1861. *
  1862. * We use ENOSPC in our driver to mean that we have run out of aperture
  1863. * space and so want to translate the error from shmemfs back to our
  1864. * usual understanding of ENOMEM.
  1865. */
  1866. if (PTR_ERR(page) == -ENOSPC)
  1867. return -ENOMEM;
  1868. else
  1869. return PTR_ERR(page);
  1870. }
  1871. /* Ensure that the associated pages are gathered from the backing storage
  1872. * and pinned into our object. i915_gem_object_get_pages() may be called
  1873. * multiple times before they are released by a single call to
  1874. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1875. * either as a result of memory pressure (reaping pages under the shrinker)
  1876. * or as the object is itself released.
  1877. */
  1878. int
  1879. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1880. {
  1881. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1882. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1883. int ret;
  1884. if (obj->pages)
  1885. return 0;
  1886. if (obj->madv != I915_MADV_WILLNEED) {
  1887. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1888. return -EFAULT;
  1889. }
  1890. BUG_ON(obj->pages_pin_count);
  1891. ret = ops->get_pages(obj);
  1892. if (ret)
  1893. return ret;
  1894. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1895. return 0;
  1896. }
  1897. static void
  1898. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1899. struct intel_engine_cs *ring)
  1900. {
  1901. struct drm_i915_gem_request *req;
  1902. struct intel_engine_cs *old_ring;
  1903. BUG_ON(ring == NULL);
  1904. req = intel_ring_get_request(ring);
  1905. old_ring = i915_gem_request_get_ring(obj->last_read_req);
  1906. if (old_ring != ring && obj->last_write_req) {
  1907. /* Keep the request relative to the current ring */
  1908. i915_gem_request_assign(&obj->last_write_req, req);
  1909. }
  1910. /* Add a reference if we're newly entering the active list. */
  1911. if (!obj->active) {
  1912. drm_gem_object_reference(&obj->base);
  1913. obj->active = 1;
  1914. }
  1915. list_move_tail(&obj->ring_list, &ring->active_list);
  1916. i915_gem_request_assign(&obj->last_read_req, req);
  1917. }
  1918. void i915_vma_move_to_active(struct i915_vma *vma,
  1919. struct intel_engine_cs *ring)
  1920. {
  1921. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  1922. return i915_gem_object_move_to_active(vma->obj, ring);
  1923. }
  1924. static void
  1925. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1926. {
  1927. struct i915_vma *vma;
  1928. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1929. BUG_ON(!obj->active);
  1930. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  1931. if (!list_empty(&vma->mm_list))
  1932. list_move_tail(&vma->mm_list, &vma->vm->inactive_list);
  1933. }
  1934. intel_fb_obj_flush(obj, true);
  1935. list_del_init(&obj->ring_list);
  1936. i915_gem_request_assign(&obj->last_read_req, NULL);
  1937. i915_gem_request_assign(&obj->last_write_req, NULL);
  1938. obj->base.write_domain = 0;
  1939. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  1940. obj->active = 0;
  1941. drm_gem_object_unreference(&obj->base);
  1942. WARN_ON(i915_verify_lists(dev));
  1943. }
  1944. static void
  1945. i915_gem_object_retire(struct drm_i915_gem_object *obj)
  1946. {
  1947. if (obj->last_read_req == NULL)
  1948. return;
  1949. if (i915_gem_request_completed(obj->last_read_req, true))
  1950. i915_gem_object_move_to_inactive(obj);
  1951. }
  1952. static int
  1953. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1954. {
  1955. struct drm_i915_private *dev_priv = dev->dev_private;
  1956. struct intel_engine_cs *ring;
  1957. int ret, i, j;
  1958. /* Carefully retire all requests without writing to the rings */
  1959. for_each_ring(ring, dev_priv, i) {
  1960. ret = intel_ring_idle(ring);
  1961. if (ret)
  1962. return ret;
  1963. }
  1964. i915_gem_retire_requests(dev);
  1965. /* Finally reset hw state */
  1966. for_each_ring(ring, dev_priv, i) {
  1967. intel_ring_init_seqno(ring, seqno);
  1968. for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  1969. ring->semaphore.sync_seqno[j] = 0;
  1970. }
  1971. return 0;
  1972. }
  1973. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1974. {
  1975. struct drm_i915_private *dev_priv = dev->dev_private;
  1976. int ret;
  1977. if (seqno == 0)
  1978. return -EINVAL;
  1979. /* HWS page needs to be set less than what we
  1980. * will inject to ring
  1981. */
  1982. ret = i915_gem_init_seqno(dev, seqno - 1);
  1983. if (ret)
  1984. return ret;
  1985. /* Carefully set the last_seqno value so that wrap
  1986. * detection still works
  1987. */
  1988. dev_priv->next_seqno = seqno;
  1989. dev_priv->last_seqno = seqno - 1;
  1990. if (dev_priv->last_seqno == 0)
  1991. dev_priv->last_seqno--;
  1992. return 0;
  1993. }
  1994. int
  1995. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1996. {
  1997. struct drm_i915_private *dev_priv = dev->dev_private;
  1998. /* reserve 0 for non-seqno */
  1999. if (dev_priv->next_seqno == 0) {
  2000. int ret = i915_gem_init_seqno(dev, 0);
  2001. if (ret)
  2002. return ret;
  2003. dev_priv->next_seqno = 1;
  2004. }
  2005. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  2006. return 0;
  2007. }
  2008. int __i915_add_request(struct intel_engine_cs *ring,
  2009. struct drm_file *file,
  2010. struct drm_i915_gem_object *obj)
  2011. {
  2012. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  2013. struct drm_i915_gem_request *request;
  2014. struct intel_ringbuffer *ringbuf;
  2015. u32 request_start;
  2016. int ret;
  2017. request = ring->outstanding_lazy_request;
  2018. if (WARN_ON(request == NULL))
  2019. return -ENOMEM;
  2020. if (i915.enable_execlists) {
  2021. ringbuf = request->ctx->engine[ring->id].ringbuf;
  2022. } else
  2023. ringbuf = ring->buffer;
  2024. request_start = intel_ring_get_tail(ringbuf);
  2025. /*
  2026. * Emit any outstanding flushes - execbuf can fail to emit the flush
  2027. * after having emitted the batchbuffer command. Hence we need to fix
  2028. * things up similar to emitting the lazy request. The difference here
  2029. * is that the flush _must_ happen before the next request, no matter
  2030. * what.
  2031. */
  2032. if (i915.enable_execlists) {
  2033. ret = logical_ring_flush_all_caches(ringbuf, request->ctx);
  2034. if (ret)
  2035. return ret;
  2036. } else {
  2037. ret = intel_ring_flush_all_caches(ring);
  2038. if (ret)
  2039. return ret;
  2040. }
  2041. /* Record the position of the start of the request so that
  2042. * should we detect the updated seqno part-way through the
  2043. * GPU processing the request, we never over-estimate the
  2044. * position of the head.
  2045. */
  2046. request->postfix = intel_ring_get_tail(ringbuf);
  2047. if (i915.enable_execlists) {
  2048. ret = ring->emit_request(ringbuf, request);
  2049. if (ret)
  2050. return ret;
  2051. } else {
  2052. ret = ring->add_request(ring);
  2053. if (ret)
  2054. return ret;
  2055. }
  2056. request->head = request_start;
  2057. request->tail = intel_ring_get_tail(ringbuf);
  2058. /* Whilst this request exists, batch_obj will be on the
  2059. * active_list, and so will hold the active reference. Only when this
  2060. * request is retired will the the batch_obj be moved onto the
  2061. * inactive_list and lose its active reference. Hence we do not need
  2062. * to explicitly hold another reference here.
  2063. */
  2064. request->batch_obj = obj;
  2065. if (!i915.enable_execlists) {
  2066. /* Hold a reference to the current context so that we can inspect
  2067. * it later in case a hangcheck error event fires.
  2068. */
  2069. request->ctx = ring->last_context;
  2070. if (request->ctx)
  2071. i915_gem_context_reference(request->ctx);
  2072. }
  2073. request->emitted_jiffies = jiffies;
  2074. list_add_tail(&request->list, &ring->request_list);
  2075. request->file_priv = NULL;
  2076. if (file) {
  2077. struct drm_i915_file_private *file_priv = file->driver_priv;
  2078. spin_lock(&file_priv->mm.lock);
  2079. request->file_priv = file_priv;
  2080. list_add_tail(&request->client_list,
  2081. &file_priv->mm.request_list);
  2082. spin_unlock(&file_priv->mm.lock);
  2083. }
  2084. trace_i915_gem_request_add(request);
  2085. ring->outstanding_lazy_request = NULL;
  2086. i915_queue_hangcheck(ring->dev);
  2087. cancel_delayed_work_sync(&dev_priv->mm.idle_work);
  2088. queue_delayed_work(dev_priv->wq,
  2089. &dev_priv->mm.retire_work,
  2090. round_jiffies_up_relative(HZ));
  2091. intel_mark_busy(dev_priv->dev);
  2092. return 0;
  2093. }
  2094. static inline void
  2095. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  2096. {
  2097. struct drm_i915_file_private *file_priv = request->file_priv;
  2098. if (!file_priv)
  2099. return;
  2100. spin_lock(&file_priv->mm.lock);
  2101. list_del(&request->client_list);
  2102. request->file_priv = NULL;
  2103. spin_unlock(&file_priv->mm.lock);
  2104. }
  2105. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2106. const struct intel_context *ctx)
  2107. {
  2108. unsigned long elapsed;
  2109. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2110. if (ctx->hang_stats.banned)
  2111. return true;
  2112. if (ctx->hang_stats.ban_period_seconds &&
  2113. elapsed <= ctx->hang_stats.ban_period_seconds) {
  2114. if (!i915_gem_context_is_default(ctx)) {
  2115. DRM_DEBUG("context hanging too fast, banning!\n");
  2116. return true;
  2117. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2118. if (i915_stop_ring_allow_warn(dev_priv))
  2119. DRM_ERROR("gpu hanging too fast, banning!\n");
  2120. return true;
  2121. }
  2122. }
  2123. return false;
  2124. }
  2125. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2126. struct intel_context *ctx,
  2127. const bool guilty)
  2128. {
  2129. struct i915_ctx_hang_stats *hs;
  2130. if (WARN_ON(!ctx))
  2131. return;
  2132. hs = &ctx->hang_stats;
  2133. if (guilty) {
  2134. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2135. hs->batch_active++;
  2136. hs->guilty_ts = get_seconds();
  2137. } else {
  2138. hs->batch_pending++;
  2139. }
  2140. }
  2141. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  2142. {
  2143. list_del(&request->list);
  2144. i915_gem_request_remove_from_client(request);
  2145. i915_gem_request_unreference(request);
  2146. }
  2147. void i915_gem_request_free(struct kref *req_ref)
  2148. {
  2149. struct drm_i915_gem_request *req = container_of(req_ref,
  2150. typeof(*req), ref);
  2151. struct intel_context *ctx = req->ctx;
  2152. if (ctx) {
  2153. if (i915.enable_execlists) {
  2154. struct intel_engine_cs *ring = req->ring;
  2155. if (ctx != ring->default_context)
  2156. intel_lr_context_unpin(ring, ctx);
  2157. }
  2158. i915_gem_context_unreference(ctx);
  2159. }
  2160. kfree(req);
  2161. }
  2162. struct drm_i915_gem_request *
  2163. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2164. {
  2165. struct drm_i915_gem_request *request;
  2166. list_for_each_entry(request, &ring->request_list, list) {
  2167. if (i915_gem_request_completed(request, false))
  2168. continue;
  2169. return request;
  2170. }
  2171. return NULL;
  2172. }
  2173. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2174. struct intel_engine_cs *ring)
  2175. {
  2176. struct drm_i915_gem_request *request;
  2177. bool ring_hung;
  2178. request = i915_gem_find_active_request(ring);
  2179. if (request == NULL)
  2180. return;
  2181. ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2182. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2183. list_for_each_entry_continue(request, &ring->request_list, list)
  2184. i915_set_reset_status(dev_priv, request->ctx, false);
  2185. }
  2186. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2187. struct intel_engine_cs *ring)
  2188. {
  2189. while (!list_empty(&ring->active_list)) {
  2190. struct drm_i915_gem_object *obj;
  2191. obj = list_first_entry(&ring->active_list,
  2192. struct drm_i915_gem_object,
  2193. ring_list);
  2194. i915_gem_object_move_to_inactive(obj);
  2195. }
  2196. /*
  2197. * Clear the execlists queue up before freeing the requests, as those
  2198. * are the ones that keep the context and ringbuffer backing objects
  2199. * pinned in place.
  2200. */
  2201. while (!list_empty(&ring->execlist_queue)) {
  2202. struct drm_i915_gem_request *submit_req;
  2203. submit_req = list_first_entry(&ring->execlist_queue,
  2204. struct drm_i915_gem_request,
  2205. execlist_link);
  2206. list_del(&submit_req->execlist_link);
  2207. intel_runtime_pm_put(dev_priv);
  2208. if (submit_req->ctx != ring->default_context)
  2209. intel_lr_context_unpin(ring, submit_req->ctx);
  2210. i915_gem_context_unreference(submit_req->ctx);
  2211. kfree(submit_req);
  2212. }
  2213. /*
  2214. * We must free the requests after all the corresponding objects have
  2215. * been moved off active lists. Which is the same order as the normal
  2216. * retire_requests function does. This is important if object hold
  2217. * implicit references on things like e.g. ppgtt address spaces through
  2218. * the request.
  2219. */
  2220. while (!list_empty(&ring->request_list)) {
  2221. struct drm_i915_gem_request *request;
  2222. request = list_first_entry(&ring->request_list,
  2223. struct drm_i915_gem_request,
  2224. list);
  2225. i915_gem_free_request(request);
  2226. }
  2227. /* This may not have been flushed before the reset, so clean it now */
  2228. i915_gem_request_assign(&ring->outstanding_lazy_request, NULL);
  2229. }
  2230. void i915_gem_restore_fences(struct drm_device *dev)
  2231. {
  2232. struct drm_i915_private *dev_priv = dev->dev_private;
  2233. int i;
  2234. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  2235. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  2236. /*
  2237. * Commit delayed tiling changes if we have an object still
  2238. * attached to the fence, otherwise just clear the fence.
  2239. */
  2240. if (reg->obj) {
  2241. i915_gem_object_update_fence(reg->obj, reg,
  2242. reg->obj->tiling_mode);
  2243. } else {
  2244. i915_gem_write_fence(dev, i, NULL);
  2245. }
  2246. }
  2247. }
  2248. void i915_gem_reset(struct drm_device *dev)
  2249. {
  2250. struct drm_i915_private *dev_priv = dev->dev_private;
  2251. struct intel_engine_cs *ring;
  2252. int i;
  2253. /*
  2254. * Before we free the objects from the requests, we need to inspect
  2255. * them for finding the guilty party. As the requests only borrow
  2256. * their reference to the objects, the inspection must be done first.
  2257. */
  2258. for_each_ring(ring, dev_priv, i)
  2259. i915_gem_reset_ring_status(dev_priv, ring);
  2260. for_each_ring(ring, dev_priv, i)
  2261. i915_gem_reset_ring_cleanup(dev_priv, ring);
  2262. i915_gem_context_reset(dev);
  2263. i915_gem_restore_fences(dev);
  2264. }
  2265. /**
  2266. * This function clears the request list as sequence numbers are passed.
  2267. */
  2268. void
  2269. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2270. {
  2271. if (list_empty(&ring->request_list))
  2272. return;
  2273. WARN_ON(i915_verify_lists(ring->dev));
  2274. /* Move any buffers on the active list that are no longer referenced
  2275. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2276. * before we free the context associated with the requests.
  2277. */
  2278. while (!list_empty(&ring->active_list)) {
  2279. struct drm_i915_gem_object *obj;
  2280. obj = list_first_entry(&ring->active_list,
  2281. struct drm_i915_gem_object,
  2282. ring_list);
  2283. if (!i915_gem_request_completed(obj->last_read_req, true))
  2284. break;
  2285. i915_gem_object_move_to_inactive(obj);
  2286. }
  2287. while (!list_empty(&ring->request_list)) {
  2288. struct drm_i915_gem_request *request;
  2289. struct intel_ringbuffer *ringbuf;
  2290. request = list_first_entry(&ring->request_list,
  2291. struct drm_i915_gem_request,
  2292. list);
  2293. if (!i915_gem_request_completed(request, true))
  2294. break;
  2295. trace_i915_gem_request_retire(request);
  2296. /* This is one of the few common intersection points
  2297. * between legacy ringbuffer submission and execlists:
  2298. * we need to tell them apart in order to find the correct
  2299. * ringbuffer to which the request belongs to.
  2300. */
  2301. if (i915.enable_execlists) {
  2302. struct intel_context *ctx = request->ctx;
  2303. ringbuf = ctx->engine[ring->id].ringbuf;
  2304. } else
  2305. ringbuf = ring->buffer;
  2306. /* We know the GPU must have read the request to have
  2307. * sent us the seqno + interrupt, so use the position
  2308. * of tail of the request to update the last known position
  2309. * of the GPU head.
  2310. */
  2311. ringbuf->last_retired_head = request->postfix;
  2312. i915_gem_free_request(request);
  2313. }
  2314. if (unlikely(ring->trace_irq_req &&
  2315. i915_gem_request_completed(ring->trace_irq_req, true))) {
  2316. ring->irq_put(ring);
  2317. i915_gem_request_assign(&ring->trace_irq_req, NULL);
  2318. }
  2319. WARN_ON(i915_verify_lists(ring->dev));
  2320. }
  2321. bool
  2322. i915_gem_retire_requests(struct drm_device *dev)
  2323. {
  2324. struct drm_i915_private *dev_priv = dev->dev_private;
  2325. struct intel_engine_cs *ring;
  2326. bool idle = true;
  2327. int i;
  2328. for_each_ring(ring, dev_priv, i) {
  2329. i915_gem_retire_requests_ring(ring);
  2330. idle &= list_empty(&ring->request_list);
  2331. if (i915.enable_execlists) {
  2332. unsigned long flags;
  2333. spin_lock_irqsave(&ring->execlist_lock, flags);
  2334. idle &= list_empty(&ring->execlist_queue);
  2335. spin_unlock_irqrestore(&ring->execlist_lock, flags);
  2336. intel_execlists_retire_requests(ring);
  2337. }
  2338. }
  2339. if (idle)
  2340. mod_delayed_work(dev_priv->wq,
  2341. &dev_priv->mm.idle_work,
  2342. msecs_to_jiffies(100));
  2343. return idle;
  2344. }
  2345. static void
  2346. i915_gem_retire_work_handler(struct work_struct *work)
  2347. {
  2348. struct drm_i915_private *dev_priv =
  2349. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2350. struct drm_device *dev = dev_priv->dev;
  2351. bool idle;
  2352. /* Come back later if the device is busy... */
  2353. idle = false;
  2354. if (mutex_trylock(&dev->struct_mutex)) {
  2355. idle = i915_gem_retire_requests(dev);
  2356. mutex_unlock(&dev->struct_mutex);
  2357. }
  2358. if (!idle)
  2359. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2360. round_jiffies_up_relative(HZ));
  2361. }
  2362. static void
  2363. i915_gem_idle_work_handler(struct work_struct *work)
  2364. {
  2365. struct drm_i915_private *dev_priv =
  2366. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2367. intel_mark_idle(dev_priv->dev);
  2368. }
  2369. /**
  2370. * Ensures that an object will eventually get non-busy by flushing any required
  2371. * write domains, emitting any outstanding lazy request and retiring and
  2372. * completed requests.
  2373. */
  2374. static int
  2375. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2376. {
  2377. struct intel_engine_cs *ring;
  2378. int ret;
  2379. if (obj->active) {
  2380. ring = i915_gem_request_get_ring(obj->last_read_req);
  2381. ret = i915_gem_check_olr(obj->last_read_req);
  2382. if (ret)
  2383. return ret;
  2384. i915_gem_retire_requests_ring(ring);
  2385. }
  2386. return 0;
  2387. }
  2388. /**
  2389. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2390. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2391. *
  2392. * Returns 0 if successful, else an error is returned with the remaining time in
  2393. * the timeout parameter.
  2394. * -ETIME: object is still busy after timeout
  2395. * -ERESTARTSYS: signal interrupted the wait
  2396. * -ENONENT: object doesn't exist
  2397. * Also possible, but rare:
  2398. * -EAGAIN: GPU wedged
  2399. * -ENOMEM: damn
  2400. * -ENODEV: Internal IRQ fail
  2401. * -E?: The add request failed
  2402. *
  2403. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2404. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2405. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2406. * without holding struct_mutex the object may become re-busied before this
  2407. * function completes. A similar but shorter * race condition exists in the busy
  2408. * ioctl
  2409. */
  2410. int
  2411. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2412. {
  2413. struct drm_i915_private *dev_priv = dev->dev_private;
  2414. struct drm_i915_gem_wait *args = data;
  2415. struct drm_i915_gem_object *obj;
  2416. struct drm_i915_gem_request *req;
  2417. unsigned reset_counter;
  2418. int ret = 0;
  2419. if (args->flags != 0)
  2420. return -EINVAL;
  2421. ret = i915_mutex_lock_interruptible(dev);
  2422. if (ret)
  2423. return ret;
  2424. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2425. if (&obj->base == NULL) {
  2426. mutex_unlock(&dev->struct_mutex);
  2427. return -ENOENT;
  2428. }
  2429. /* Need to make sure the object gets inactive eventually. */
  2430. ret = i915_gem_object_flush_active(obj);
  2431. if (ret)
  2432. goto out;
  2433. if (!obj->active || !obj->last_read_req)
  2434. goto out;
  2435. req = obj->last_read_req;
  2436. /* Do this after OLR check to make sure we make forward progress polling
  2437. * on this IOCTL with a timeout <=0 (like busy ioctl)
  2438. */
  2439. if (args->timeout_ns <= 0) {
  2440. ret = -ETIME;
  2441. goto out;
  2442. }
  2443. drm_gem_object_unreference(&obj->base);
  2444. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2445. i915_gem_request_reference(req);
  2446. mutex_unlock(&dev->struct_mutex);
  2447. ret = __i915_wait_request(req, reset_counter, true, &args->timeout_ns,
  2448. file->driver_priv);
  2449. mutex_lock(&dev->struct_mutex);
  2450. i915_gem_request_unreference(req);
  2451. mutex_unlock(&dev->struct_mutex);
  2452. return ret;
  2453. out:
  2454. drm_gem_object_unreference(&obj->base);
  2455. mutex_unlock(&dev->struct_mutex);
  2456. return ret;
  2457. }
  2458. /**
  2459. * i915_gem_object_sync - sync an object to a ring.
  2460. *
  2461. * @obj: object which may be in use on another ring.
  2462. * @to: ring we wish to use the object on. May be NULL.
  2463. *
  2464. * This code is meant to abstract object synchronization with the GPU.
  2465. * Calling with NULL implies synchronizing the object with the CPU
  2466. * rather than a particular GPU ring.
  2467. *
  2468. * Returns 0 if successful, else propagates up the lower layer error.
  2469. */
  2470. int
  2471. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2472. struct intel_engine_cs *to)
  2473. {
  2474. struct intel_engine_cs *from;
  2475. u32 seqno;
  2476. int ret, idx;
  2477. from = i915_gem_request_get_ring(obj->last_read_req);
  2478. if (from == NULL || to == from)
  2479. return 0;
  2480. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2481. return i915_gem_object_wait_rendering(obj, false);
  2482. idx = intel_ring_sync_index(from, to);
  2483. seqno = i915_gem_request_get_seqno(obj->last_read_req);
  2484. /* Optimization: Avoid semaphore sync when we are sure we already
  2485. * waited for an object with higher seqno */
  2486. if (seqno <= from->semaphore.sync_seqno[idx])
  2487. return 0;
  2488. ret = i915_gem_check_olr(obj->last_read_req);
  2489. if (ret)
  2490. return ret;
  2491. trace_i915_gem_ring_sync_to(from, to, obj->last_read_req);
  2492. ret = to->semaphore.sync_to(to, from, seqno);
  2493. if (!ret)
  2494. /* We use last_read_req because sync_to()
  2495. * might have just caused seqno wrap under
  2496. * the radar.
  2497. */
  2498. from->semaphore.sync_seqno[idx] =
  2499. i915_gem_request_get_seqno(obj->last_read_req);
  2500. return ret;
  2501. }
  2502. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2503. {
  2504. u32 old_write_domain, old_read_domains;
  2505. /* Force a pagefault for domain tracking on next user access */
  2506. i915_gem_release_mmap(obj);
  2507. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2508. return;
  2509. /* Wait for any direct GTT access to complete */
  2510. mb();
  2511. old_read_domains = obj->base.read_domains;
  2512. old_write_domain = obj->base.write_domain;
  2513. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2514. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2515. trace_i915_gem_object_change_domain(obj,
  2516. old_read_domains,
  2517. old_write_domain);
  2518. }
  2519. int i915_vma_unbind(struct i915_vma *vma)
  2520. {
  2521. struct drm_i915_gem_object *obj = vma->obj;
  2522. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2523. int ret;
  2524. if (list_empty(&vma->vma_link))
  2525. return 0;
  2526. if (!drm_mm_node_allocated(&vma->node)) {
  2527. i915_gem_vma_destroy(vma);
  2528. return 0;
  2529. }
  2530. if (vma->pin_count)
  2531. return -EBUSY;
  2532. BUG_ON(obj->pages == NULL);
  2533. ret = i915_gem_object_finish_gpu(obj);
  2534. if (ret)
  2535. return ret;
  2536. /* Continue on if we fail due to EIO, the GPU is hung so we
  2537. * should be safe and we need to cleanup or else we might
  2538. * cause memory corruption through use-after-free.
  2539. */
  2540. if (i915_is_ggtt(vma->vm) &&
  2541. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2542. i915_gem_object_finish_gtt(obj);
  2543. /* release the fence reg _after_ flushing */
  2544. ret = i915_gem_object_put_fence(obj);
  2545. if (ret)
  2546. return ret;
  2547. }
  2548. trace_i915_vma_unbind(vma);
  2549. vma->unbind_vma(vma);
  2550. list_del_init(&vma->mm_list);
  2551. if (i915_is_ggtt(vma->vm)) {
  2552. if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) {
  2553. obj->map_and_fenceable = false;
  2554. } else if (vma->ggtt_view.pages) {
  2555. sg_free_table(vma->ggtt_view.pages);
  2556. kfree(vma->ggtt_view.pages);
  2557. vma->ggtt_view.pages = NULL;
  2558. }
  2559. }
  2560. drm_mm_remove_node(&vma->node);
  2561. i915_gem_vma_destroy(vma);
  2562. /* Since the unbound list is global, only move to that list if
  2563. * no more VMAs exist. */
  2564. if (list_empty(&obj->vma_list)) {
  2565. /* Throw away the active reference before
  2566. * moving to the unbound list. */
  2567. i915_gem_object_retire(obj);
  2568. i915_gem_gtt_finish_object(obj);
  2569. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2570. }
  2571. /* And finally now the object is completely decoupled from this vma,
  2572. * we can drop its hold on the backing storage and allow it to be
  2573. * reaped by the shrinker.
  2574. */
  2575. i915_gem_object_unpin_pages(obj);
  2576. return 0;
  2577. }
  2578. int i915_gpu_idle(struct drm_device *dev)
  2579. {
  2580. struct drm_i915_private *dev_priv = dev->dev_private;
  2581. struct intel_engine_cs *ring;
  2582. int ret, i;
  2583. /* Flush everything onto the inactive list. */
  2584. for_each_ring(ring, dev_priv, i) {
  2585. if (!i915.enable_execlists) {
  2586. ret = i915_switch_context(ring, ring->default_context);
  2587. if (ret)
  2588. return ret;
  2589. }
  2590. ret = intel_ring_idle(ring);
  2591. if (ret)
  2592. return ret;
  2593. }
  2594. return 0;
  2595. }
  2596. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2597. struct drm_i915_gem_object *obj)
  2598. {
  2599. struct drm_i915_private *dev_priv = dev->dev_private;
  2600. int fence_reg;
  2601. int fence_pitch_shift;
  2602. if (INTEL_INFO(dev)->gen >= 6) {
  2603. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2604. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2605. } else {
  2606. fence_reg = FENCE_REG_965_0;
  2607. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2608. }
  2609. fence_reg += reg * 8;
  2610. /* To w/a incoherency with non-atomic 64-bit register updates,
  2611. * we split the 64-bit update into two 32-bit writes. In order
  2612. * for a partial fence not to be evaluated between writes, we
  2613. * precede the update with write to turn off the fence register,
  2614. * and only enable the fence as the last step.
  2615. *
  2616. * For extra levels of paranoia, we make sure each step lands
  2617. * before applying the next step.
  2618. */
  2619. I915_WRITE(fence_reg, 0);
  2620. POSTING_READ(fence_reg);
  2621. if (obj) {
  2622. u32 size = i915_gem_obj_ggtt_size(obj);
  2623. uint64_t val;
  2624. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2625. 0xfffff000) << 32;
  2626. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2627. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2628. if (obj->tiling_mode == I915_TILING_Y)
  2629. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2630. val |= I965_FENCE_REG_VALID;
  2631. I915_WRITE(fence_reg + 4, val >> 32);
  2632. POSTING_READ(fence_reg + 4);
  2633. I915_WRITE(fence_reg + 0, val);
  2634. POSTING_READ(fence_reg);
  2635. } else {
  2636. I915_WRITE(fence_reg + 4, 0);
  2637. POSTING_READ(fence_reg + 4);
  2638. }
  2639. }
  2640. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2641. struct drm_i915_gem_object *obj)
  2642. {
  2643. struct drm_i915_private *dev_priv = dev->dev_private;
  2644. u32 val;
  2645. if (obj) {
  2646. u32 size = i915_gem_obj_ggtt_size(obj);
  2647. int pitch_val;
  2648. int tile_width;
  2649. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2650. (size & -size) != size ||
  2651. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2652. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2653. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2654. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2655. tile_width = 128;
  2656. else
  2657. tile_width = 512;
  2658. /* Note: pitch better be a power of two tile widths */
  2659. pitch_val = obj->stride / tile_width;
  2660. pitch_val = ffs(pitch_val) - 1;
  2661. val = i915_gem_obj_ggtt_offset(obj);
  2662. if (obj->tiling_mode == I915_TILING_Y)
  2663. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2664. val |= I915_FENCE_SIZE_BITS(size);
  2665. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2666. val |= I830_FENCE_REG_VALID;
  2667. } else
  2668. val = 0;
  2669. if (reg < 8)
  2670. reg = FENCE_REG_830_0 + reg * 4;
  2671. else
  2672. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2673. I915_WRITE(reg, val);
  2674. POSTING_READ(reg);
  2675. }
  2676. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2677. struct drm_i915_gem_object *obj)
  2678. {
  2679. struct drm_i915_private *dev_priv = dev->dev_private;
  2680. uint32_t val;
  2681. if (obj) {
  2682. u32 size = i915_gem_obj_ggtt_size(obj);
  2683. uint32_t pitch_val;
  2684. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2685. (size & -size) != size ||
  2686. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2687. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2688. i915_gem_obj_ggtt_offset(obj), size);
  2689. pitch_val = obj->stride / 128;
  2690. pitch_val = ffs(pitch_val) - 1;
  2691. val = i915_gem_obj_ggtt_offset(obj);
  2692. if (obj->tiling_mode == I915_TILING_Y)
  2693. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2694. val |= I830_FENCE_SIZE_BITS(size);
  2695. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2696. val |= I830_FENCE_REG_VALID;
  2697. } else
  2698. val = 0;
  2699. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2700. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2701. }
  2702. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2703. {
  2704. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2705. }
  2706. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2707. struct drm_i915_gem_object *obj)
  2708. {
  2709. struct drm_i915_private *dev_priv = dev->dev_private;
  2710. /* Ensure that all CPU reads are completed before installing a fence
  2711. * and all writes before removing the fence.
  2712. */
  2713. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2714. mb();
  2715. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2716. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2717. obj->stride, obj->tiling_mode);
  2718. if (IS_GEN2(dev))
  2719. i830_write_fence_reg(dev, reg, obj);
  2720. else if (IS_GEN3(dev))
  2721. i915_write_fence_reg(dev, reg, obj);
  2722. else if (INTEL_INFO(dev)->gen >= 4)
  2723. i965_write_fence_reg(dev, reg, obj);
  2724. /* And similarly be paranoid that no direct access to this region
  2725. * is reordered to before the fence is installed.
  2726. */
  2727. if (i915_gem_object_needs_mb(obj))
  2728. mb();
  2729. }
  2730. static inline int fence_number(struct drm_i915_private *dev_priv,
  2731. struct drm_i915_fence_reg *fence)
  2732. {
  2733. return fence - dev_priv->fence_regs;
  2734. }
  2735. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2736. struct drm_i915_fence_reg *fence,
  2737. bool enable)
  2738. {
  2739. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2740. int reg = fence_number(dev_priv, fence);
  2741. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2742. if (enable) {
  2743. obj->fence_reg = reg;
  2744. fence->obj = obj;
  2745. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2746. } else {
  2747. obj->fence_reg = I915_FENCE_REG_NONE;
  2748. fence->obj = NULL;
  2749. list_del_init(&fence->lru_list);
  2750. }
  2751. obj->fence_dirty = false;
  2752. }
  2753. static int
  2754. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2755. {
  2756. if (obj->last_fenced_req) {
  2757. int ret = i915_wait_request(obj->last_fenced_req);
  2758. if (ret)
  2759. return ret;
  2760. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  2761. }
  2762. return 0;
  2763. }
  2764. int
  2765. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2766. {
  2767. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2768. struct drm_i915_fence_reg *fence;
  2769. int ret;
  2770. ret = i915_gem_object_wait_fence(obj);
  2771. if (ret)
  2772. return ret;
  2773. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2774. return 0;
  2775. fence = &dev_priv->fence_regs[obj->fence_reg];
  2776. if (WARN_ON(fence->pin_count))
  2777. return -EBUSY;
  2778. i915_gem_object_fence_lost(obj);
  2779. i915_gem_object_update_fence(obj, fence, false);
  2780. return 0;
  2781. }
  2782. static struct drm_i915_fence_reg *
  2783. i915_find_fence_reg(struct drm_device *dev)
  2784. {
  2785. struct drm_i915_private *dev_priv = dev->dev_private;
  2786. struct drm_i915_fence_reg *reg, *avail;
  2787. int i;
  2788. /* First try to find a free reg */
  2789. avail = NULL;
  2790. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2791. reg = &dev_priv->fence_regs[i];
  2792. if (!reg->obj)
  2793. return reg;
  2794. if (!reg->pin_count)
  2795. avail = reg;
  2796. }
  2797. if (avail == NULL)
  2798. goto deadlock;
  2799. /* None available, try to steal one or wait for a user to finish */
  2800. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2801. if (reg->pin_count)
  2802. continue;
  2803. return reg;
  2804. }
  2805. deadlock:
  2806. /* Wait for completion of pending flips which consume fences */
  2807. if (intel_has_pending_fb_unpin(dev))
  2808. return ERR_PTR(-EAGAIN);
  2809. return ERR_PTR(-EDEADLK);
  2810. }
  2811. /**
  2812. * i915_gem_object_get_fence - set up fencing for an object
  2813. * @obj: object to map through a fence reg
  2814. *
  2815. * When mapping objects through the GTT, userspace wants to be able to write
  2816. * to them without having to worry about swizzling if the object is tiled.
  2817. * This function walks the fence regs looking for a free one for @obj,
  2818. * stealing one if it can't find any.
  2819. *
  2820. * It then sets up the reg based on the object's properties: address, pitch
  2821. * and tiling format.
  2822. *
  2823. * For an untiled surface, this removes any existing fence.
  2824. */
  2825. int
  2826. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2827. {
  2828. struct drm_device *dev = obj->base.dev;
  2829. struct drm_i915_private *dev_priv = dev->dev_private;
  2830. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2831. struct drm_i915_fence_reg *reg;
  2832. int ret;
  2833. /* Have we updated the tiling parameters upon the object and so
  2834. * will need to serialise the write to the associated fence register?
  2835. */
  2836. if (obj->fence_dirty) {
  2837. ret = i915_gem_object_wait_fence(obj);
  2838. if (ret)
  2839. return ret;
  2840. }
  2841. /* Just update our place in the LRU if our fence is getting reused. */
  2842. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2843. reg = &dev_priv->fence_regs[obj->fence_reg];
  2844. if (!obj->fence_dirty) {
  2845. list_move_tail(&reg->lru_list,
  2846. &dev_priv->mm.fence_list);
  2847. return 0;
  2848. }
  2849. } else if (enable) {
  2850. if (WARN_ON(!obj->map_and_fenceable))
  2851. return -EINVAL;
  2852. reg = i915_find_fence_reg(dev);
  2853. if (IS_ERR(reg))
  2854. return PTR_ERR(reg);
  2855. if (reg->obj) {
  2856. struct drm_i915_gem_object *old = reg->obj;
  2857. ret = i915_gem_object_wait_fence(old);
  2858. if (ret)
  2859. return ret;
  2860. i915_gem_object_fence_lost(old);
  2861. }
  2862. } else
  2863. return 0;
  2864. i915_gem_object_update_fence(obj, reg, enable);
  2865. return 0;
  2866. }
  2867. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2868. unsigned long cache_level)
  2869. {
  2870. struct drm_mm_node *gtt_space = &vma->node;
  2871. struct drm_mm_node *other;
  2872. /*
  2873. * On some machines we have to be careful when putting differing types
  2874. * of snoopable memory together to avoid the prefetcher crossing memory
  2875. * domains and dying. During vm initialisation, we decide whether or not
  2876. * these constraints apply and set the drm_mm.color_adjust
  2877. * appropriately.
  2878. */
  2879. if (vma->vm->mm.color_adjust == NULL)
  2880. return true;
  2881. if (!drm_mm_node_allocated(gtt_space))
  2882. return true;
  2883. if (list_empty(&gtt_space->node_list))
  2884. return true;
  2885. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2886. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2887. return false;
  2888. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2889. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2890. return false;
  2891. return true;
  2892. }
  2893. /**
  2894. * Finds free space in the GTT aperture and binds the object there.
  2895. */
  2896. static struct i915_vma *
  2897. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2898. struct i915_address_space *vm,
  2899. unsigned alignment,
  2900. uint64_t flags,
  2901. const struct i915_ggtt_view *view)
  2902. {
  2903. struct drm_device *dev = obj->base.dev;
  2904. struct drm_i915_private *dev_priv = dev->dev_private;
  2905. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2906. unsigned long start =
  2907. flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2908. unsigned long end =
  2909. flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
  2910. struct i915_vma *vma;
  2911. int ret;
  2912. fence_size = i915_gem_get_gtt_size(dev,
  2913. obj->base.size,
  2914. obj->tiling_mode);
  2915. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2916. obj->base.size,
  2917. obj->tiling_mode, true);
  2918. unfenced_alignment =
  2919. i915_gem_get_gtt_alignment(dev,
  2920. obj->base.size,
  2921. obj->tiling_mode, false);
  2922. if (alignment == 0)
  2923. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2924. unfenced_alignment;
  2925. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2926. DRM_DEBUG("Invalid object alignment requested %u\n", alignment);
  2927. return ERR_PTR(-EINVAL);
  2928. }
  2929. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2930. /* If the object is bigger than the entire aperture, reject it early
  2931. * before evicting everything in a vain attempt to find space.
  2932. */
  2933. if (obj->base.size > end) {
  2934. DRM_DEBUG("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%lu\n",
  2935. obj->base.size,
  2936. flags & PIN_MAPPABLE ? "mappable" : "total",
  2937. end);
  2938. return ERR_PTR(-E2BIG);
  2939. }
  2940. ret = i915_gem_object_get_pages(obj);
  2941. if (ret)
  2942. return ERR_PTR(ret);
  2943. i915_gem_object_pin_pages(obj);
  2944. vma = i915_gem_obj_lookup_or_create_vma_view(obj, vm, view);
  2945. if (IS_ERR(vma))
  2946. goto err_unpin;
  2947. search_free:
  2948. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2949. size, alignment,
  2950. obj->cache_level,
  2951. start, end,
  2952. DRM_MM_SEARCH_DEFAULT,
  2953. DRM_MM_CREATE_DEFAULT);
  2954. if (ret) {
  2955. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2956. obj->cache_level,
  2957. start, end,
  2958. flags);
  2959. if (ret == 0)
  2960. goto search_free;
  2961. goto err_free_vma;
  2962. }
  2963. if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  2964. ret = -EINVAL;
  2965. goto err_remove_node;
  2966. }
  2967. ret = i915_gem_gtt_prepare_object(obj);
  2968. if (ret)
  2969. goto err_remove_node;
  2970. trace_i915_vma_bind(vma, flags);
  2971. ret = i915_vma_bind(vma, obj->cache_level,
  2972. flags & PIN_GLOBAL ? GLOBAL_BIND : 0);
  2973. if (ret)
  2974. goto err_finish_gtt;
  2975. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2976. list_add_tail(&vma->mm_list, &vm->inactive_list);
  2977. return vma;
  2978. err_finish_gtt:
  2979. i915_gem_gtt_finish_object(obj);
  2980. err_remove_node:
  2981. drm_mm_remove_node(&vma->node);
  2982. err_free_vma:
  2983. i915_gem_vma_destroy(vma);
  2984. vma = ERR_PTR(ret);
  2985. err_unpin:
  2986. i915_gem_object_unpin_pages(obj);
  2987. return vma;
  2988. }
  2989. bool
  2990. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2991. bool force)
  2992. {
  2993. /* If we don't have a page list set up, then we're not pinned
  2994. * to GPU, and we can ignore the cache flush because it'll happen
  2995. * again at bind time.
  2996. */
  2997. if (obj->pages == NULL)
  2998. return false;
  2999. /*
  3000. * Stolen memory is always coherent with the GPU as it is explicitly
  3001. * marked as wc by the system, or the system is cache-coherent.
  3002. */
  3003. if (obj->stolen || obj->phys_handle)
  3004. return false;
  3005. /* If the GPU is snooping the contents of the CPU cache,
  3006. * we do not need to manually clear the CPU cache lines. However,
  3007. * the caches are only snooped when the render cache is
  3008. * flushed/invalidated. As we always have to emit invalidations
  3009. * and flushes when moving into and out of the RENDER domain, correct
  3010. * snooping behaviour occurs naturally as the result of our domain
  3011. * tracking.
  3012. */
  3013. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  3014. return false;
  3015. trace_i915_gem_object_clflush(obj);
  3016. drm_clflush_sg(obj->pages);
  3017. return true;
  3018. }
  3019. /** Flushes the GTT write domain for the object if it's dirty. */
  3020. static void
  3021. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3022. {
  3023. uint32_t old_write_domain;
  3024. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3025. return;
  3026. /* No actual flushing is required for the GTT write domain. Writes
  3027. * to it immediately go to main memory as far as we know, so there's
  3028. * no chipset flush. It also doesn't land in render cache.
  3029. *
  3030. * However, we do have to enforce the order so that all writes through
  3031. * the GTT land before any writes to the device, such as updates to
  3032. * the GATT itself.
  3033. */
  3034. wmb();
  3035. old_write_domain = obj->base.write_domain;
  3036. obj->base.write_domain = 0;
  3037. intel_fb_obj_flush(obj, false);
  3038. trace_i915_gem_object_change_domain(obj,
  3039. obj->base.read_domains,
  3040. old_write_domain);
  3041. }
  3042. /** Flushes the CPU write domain for the object if it's dirty. */
  3043. static void
  3044. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  3045. bool force)
  3046. {
  3047. uint32_t old_write_domain;
  3048. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3049. return;
  3050. if (i915_gem_clflush_object(obj, force))
  3051. i915_gem_chipset_flush(obj->base.dev);
  3052. old_write_domain = obj->base.write_domain;
  3053. obj->base.write_domain = 0;
  3054. intel_fb_obj_flush(obj, false);
  3055. trace_i915_gem_object_change_domain(obj,
  3056. obj->base.read_domains,
  3057. old_write_domain);
  3058. }
  3059. /**
  3060. * Moves a single object to the GTT read, and possibly write domain.
  3061. *
  3062. * This function returns when the move is complete, including waiting on
  3063. * flushes to occur.
  3064. */
  3065. int
  3066. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3067. {
  3068. uint32_t old_write_domain, old_read_domains;
  3069. struct i915_vma *vma;
  3070. int ret;
  3071. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3072. return 0;
  3073. ret = i915_gem_object_wait_rendering(obj, !write);
  3074. if (ret)
  3075. return ret;
  3076. i915_gem_object_retire(obj);
  3077. /* Flush and acquire obj->pages so that we are coherent through
  3078. * direct access in memory with previous cached writes through
  3079. * shmemfs and that our cache domain tracking remains valid.
  3080. * For example, if the obj->filp was moved to swap without us
  3081. * being notified and releasing the pages, we would mistakenly
  3082. * continue to assume that the obj remained out of the CPU cached
  3083. * domain.
  3084. */
  3085. ret = i915_gem_object_get_pages(obj);
  3086. if (ret)
  3087. return ret;
  3088. i915_gem_object_flush_cpu_write_domain(obj, false);
  3089. /* Serialise direct access to this object with the barriers for
  3090. * coherent writes from the GPU, by effectively invalidating the
  3091. * GTT domain upon first access.
  3092. */
  3093. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3094. mb();
  3095. old_write_domain = obj->base.write_domain;
  3096. old_read_domains = obj->base.read_domains;
  3097. /* It should now be out of any other write domains, and we can update
  3098. * the domain values for our changes.
  3099. */
  3100. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3101. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3102. if (write) {
  3103. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3104. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3105. obj->dirty = 1;
  3106. }
  3107. if (write)
  3108. intel_fb_obj_invalidate(obj, NULL);
  3109. trace_i915_gem_object_change_domain(obj,
  3110. old_read_domains,
  3111. old_write_domain);
  3112. /* And bump the LRU for this access */
  3113. vma = i915_gem_obj_to_ggtt(obj);
  3114. if (vma && drm_mm_node_allocated(&vma->node) && !obj->active)
  3115. list_move_tail(&vma->mm_list,
  3116. &to_i915(obj->base.dev)->gtt.base.inactive_list);
  3117. return 0;
  3118. }
  3119. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3120. enum i915_cache_level cache_level)
  3121. {
  3122. struct drm_device *dev = obj->base.dev;
  3123. struct i915_vma *vma, *next;
  3124. int ret;
  3125. if (obj->cache_level == cache_level)
  3126. return 0;
  3127. if (i915_gem_obj_is_pinned(obj)) {
  3128. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3129. return -EBUSY;
  3130. }
  3131. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3132. if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3133. ret = i915_vma_unbind(vma);
  3134. if (ret)
  3135. return ret;
  3136. }
  3137. }
  3138. if (i915_gem_obj_bound_any(obj)) {
  3139. ret = i915_gem_object_finish_gpu(obj);
  3140. if (ret)
  3141. return ret;
  3142. i915_gem_object_finish_gtt(obj);
  3143. /* Before SandyBridge, you could not use tiling or fence
  3144. * registers with snooped memory, so relinquish any fences
  3145. * currently pointing to our region in the aperture.
  3146. */
  3147. if (INTEL_INFO(dev)->gen < 6) {
  3148. ret = i915_gem_object_put_fence(obj);
  3149. if (ret)
  3150. return ret;
  3151. }
  3152. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3153. if (drm_mm_node_allocated(&vma->node)) {
  3154. ret = i915_vma_bind(vma, cache_level,
  3155. vma->bound & GLOBAL_BIND);
  3156. if (ret)
  3157. return ret;
  3158. }
  3159. }
  3160. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3161. vma->node.color = cache_level;
  3162. obj->cache_level = cache_level;
  3163. if (cpu_write_needs_clflush(obj)) {
  3164. u32 old_read_domains, old_write_domain;
  3165. /* If we're coming from LLC cached, then we haven't
  3166. * actually been tracking whether the data is in the
  3167. * CPU cache or not, since we only allow one bit set
  3168. * in obj->write_domain and have been skipping the clflushes.
  3169. * Just set it to the CPU cache for now.
  3170. */
  3171. i915_gem_object_retire(obj);
  3172. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3173. old_read_domains = obj->base.read_domains;
  3174. old_write_domain = obj->base.write_domain;
  3175. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3176. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3177. trace_i915_gem_object_change_domain(obj,
  3178. old_read_domains,
  3179. old_write_domain);
  3180. }
  3181. return 0;
  3182. }
  3183. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3184. struct drm_file *file)
  3185. {
  3186. struct drm_i915_gem_caching *args = data;
  3187. struct drm_i915_gem_object *obj;
  3188. int ret;
  3189. ret = i915_mutex_lock_interruptible(dev);
  3190. if (ret)
  3191. return ret;
  3192. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3193. if (&obj->base == NULL) {
  3194. ret = -ENOENT;
  3195. goto unlock;
  3196. }
  3197. switch (obj->cache_level) {
  3198. case I915_CACHE_LLC:
  3199. case I915_CACHE_L3_LLC:
  3200. args->caching = I915_CACHING_CACHED;
  3201. break;
  3202. case I915_CACHE_WT:
  3203. args->caching = I915_CACHING_DISPLAY;
  3204. break;
  3205. default:
  3206. args->caching = I915_CACHING_NONE;
  3207. break;
  3208. }
  3209. drm_gem_object_unreference(&obj->base);
  3210. unlock:
  3211. mutex_unlock(&dev->struct_mutex);
  3212. return ret;
  3213. }
  3214. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3215. struct drm_file *file)
  3216. {
  3217. struct drm_i915_gem_caching *args = data;
  3218. struct drm_i915_gem_object *obj;
  3219. enum i915_cache_level level;
  3220. int ret;
  3221. switch (args->caching) {
  3222. case I915_CACHING_NONE:
  3223. level = I915_CACHE_NONE;
  3224. break;
  3225. case I915_CACHING_CACHED:
  3226. level = I915_CACHE_LLC;
  3227. break;
  3228. case I915_CACHING_DISPLAY:
  3229. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3230. break;
  3231. default:
  3232. return -EINVAL;
  3233. }
  3234. ret = i915_mutex_lock_interruptible(dev);
  3235. if (ret)
  3236. return ret;
  3237. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3238. if (&obj->base == NULL) {
  3239. ret = -ENOENT;
  3240. goto unlock;
  3241. }
  3242. ret = i915_gem_object_set_cache_level(obj, level);
  3243. drm_gem_object_unreference(&obj->base);
  3244. unlock:
  3245. mutex_unlock(&dev->struct_mutex);
  3246. return ret;
  3247. }
  3248. static bool is_pin_display(struct drm_i915_gem_object *obj)
  3249. {
  3250. struct i915_vma *vma;
  3251. vma = i915_gem_obj_to_ggtt(obj);
  3252. if (!vma)
  3253. return false;
  3254. /* There are 2 sources that pin objects:
  3255. * 1. The display engine (scanouts, sprites, cursors);
  3256. * 2. Reservations for execbuffer;
  3257. *
  3258. * We can ignore reservations as we hold the struct_mutex and
  3259. * are only called outside of the reservation path.
  3260. */
  3261. return vma->pin_count;
  3262. }
  3263. /*
  3264. * Prepare buffer for display plane (scanout, cursors, etc).
  3265. * Can be called from an uninterruptible phase (modesetting) and allows
  3266. * any flushes to be pipelined (for pageflips).
  3267. */
  3268. int
  3269. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3270. u32 alignment,
  3271. struct intel_engine_cs *pipelined)
  3272. {
  3273. u32 old_read_domains, old_write_domain;
  3274. bool was_pin_display;
  3275. int ret;
  3276. if (pipelined != i915_gem_request_get_ring(obj->last_read_req)) {
  3277. ret = i915_gem_object_sync(obj, pipelined);
  3278. if (ret)
  3279. return ret;
  3280. }
  3281. /* Mark the pin_display early so that we account for the
  3282. * display coherency whilst setting up the cache domains.
  3283. */
  3284. was_pin_display = obj->pin_display;
  3285. obj->pin_display = true;
  3286. /* The display engine is not coherent with the LLC cache on gen6. As
  3287. * a result, we make sure that the pinning that is about to occur is
  3288. * done with uncached PTEs. This is lowest common denominator for all
  3289. * chipsets.
  3290. *
  3291. * However for gen6+, we could do better by using the GFDT bit instead
  3292. * of uncaching, which would allow us to flush all the LLC-cached data
  3293. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3294. */
  3295. ret = i915_gem_object_set_cache_level(obj,
  3296. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3297. if (ret)
  3298. goto err_unpin_display;
  3299. /* As the user may map the buffer once pinned in the display plane
  3300. * (e.g. libkms for the bootup splash), we have to ensure that we
  3301. * always use map_and_fenceable for all scanout buffers.
  3302. */
  3303. ret = i915_gem_obj_ggtt_pin(obj, alignment, PIN_MAPPABLE);
  3304. if (ret)
  3305. goto err_unpin_display;
  3306. i915_gem_object_flush_cpu_write_domain(obj, true);
  3307. old_write_domain = obj->base.write_domain;
  3308. old_read_domains = obj->base.read_domains;
  3309. /* It should now be out of any other write domains, and we can update
  3310. * the domain values for our changes.
  3311. */
  3312. obj->base.write_domain = 0;
  3313. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3314. trace_i915_gem_object_change_domain(obj,
  3315. old_read_domains,
  3316. old_write_domain);
  3317. return 0;
  3318. err_unpin_display:
  3319. WARN_ON(was_pin_display != is_pin_display(obj));
  3320. obj->pin_display = was_pin_display;
  3321. return ret;
  3322. }
  3323. void
  3324. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
  3325. {
  3326. i915_gem_object_ggtt_unpin(obj);
  3327. obj->pin_display = is_pin_display(obj);
  3328. }
  3329. int
  3330. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  3331. {
  3332. int ret;
  3333. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  3334. return 0;
  3335. ret = i915_gem_object_wait_rendering(obj, false);
  3336. if (ret)
  3337. return ret;
  3338. /* Ensure that we invalidate the GPU's caches and TLBs. */
  3339. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  3340. return 0;
  3341. }
  3342. /**
  3343. * Moves a single object to the CPU read, and possibly write domain.
  3344. *
  3345. * This function returns when the move is complete, including waiting on
  3346. * flushes to occur.
  3347. */
  3348. int
  3349. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3350. {
  3351. uint32_t old_write_domain, old_read_domains;
  3352. int ret;
  3353. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3354. return 0;
  3355. ret = i915_gem_object_wait_rendering(obj, !write);
  3356. if (ret)
  3357. return ret;
  3358. i915_gem_object_retire(obj);
  3359. i915_gem_object_flush_gtt_write_domain(obj);
  3360. old_write_domain = obj->base.write_domain;
  3361. old_read_domains = obj->base.read_domains;
  3362. /* Flush the CPU cache if it's still invalid. */
  3363. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3364. i915_gem_clflush_object(obj, false);
  3365. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3366. }
  3367. /* It should now be out of any other write domains, and we can update
  3368. * the domain values for our changes.
  3369. */
  3370. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3371. /* If we're writing through the CPU, then the GPU read domains will
  3372. * need to be invalidated at next use.
  3373. */
  3374. if (write) {
  3375. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3376. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3377. }
  3378. if (write)
  3379. intel_fb_obj_invalidate(obj, NULL);
  3380. trace_i915_gem_object_change_domain(obj,
  3381. old_read_domains,
  3382. old_write_domain);
  3383. return 0;
  3384. }
  3385. /* Throttle our rendering by waiting until the ring has completed our requests
  3386. * emitted over 20 msec ago.
  3387. *
  3388. * Note that if we were to use the current jiffies each time around the loop,
  3389. * we wouldn't escape the function with any frames outstanding if the time to
  3390. * render a frame was over 20ms.
  3391. *
  3392. * This should get us reasonable parallelism between CPU and GPU but also
  3393. * relatively low latency when blocking on a particular request to finish.
  3394. */
  3395. static int
  3396. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3397. {
  3398. struct drm_i915_private *dev_priv = dev->dev_private;
  3399. struct drm_i915_file_private *file_priv = file->driver_priv;
  3400. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3401. struct drm_i915_gem_request *request, *target = NULL;
  3402. unsigned reset_counter;
  3403. int ret;
  3404. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3405. if (ret)
  3406. return ret;
  3407. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3408. if (ret)
  3409. return ret;
  3410. spin_lock(&file_priv->mm.lock);
  3411. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3412. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3413. break;
  3414. target = request;
  3415. }
  3416. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3417. if (target)
  3418. i915_gem_request_reference(target);
  3419. spin_unlock(&file_priv->mm.lock);
  3420. if (target == NULL)
  3421. return 0;
  3422. ret = __i915_wait_request(target, reset_counter, true, NULL, NULL);
  3423. if (ret == 0)
  3424. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3425. mutex_lock(&dev->struct_mutex);
  3426. i915_gem_request_unreference(target);
  3427. mutex_unlock(&dev->struct_mutex);
  3428. return ret;
  3429. }
  3430. static bool
  3431. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3432. {
  3433. struct drm_i915_gem_object *obj = vma->obj;
  3434. if (alignment &&
  3435. vma->node.start & (alignment - 1))
  3436. return true;
  3437. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3438. return true;
  3439. if (flags & PIN_OFFSET_BIAS &&
  3440. vma->node.start < (flags & PIN_OFFSET_MASK))
  3441. return true;
  3442. return false;
  3443. }
  3444. int
  3445. i915_gem_object_pin_view(struct drm_i915_gem_object *obj,
  3446. struct i915_address_space *vm,
  3447. uint32_t alignment,
  3448. uint64_t flags,
  3449. const struct i915_ggtt_view *view)
  3450. {
  3451. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3452. struct i915_vma *vma;
  3453. unsigned bound;
  3454. int ret;
  3455. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3456. return -ENODEV;
  3457. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3458. return -EINVAL;
  3459. if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3460. return -EINVAL;
  3461. vma = i915_gem_obj_to_vma_view(obj, vm, view);
  3462. if (vma) {
  3463. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3464. return -EBUSY;
  3465. if (i915_vma_misplaced(vma, alignment, flags)) {
  3466. WARN(vma->pin_count,
  3467. "bo is already pinned with incorrect alignment:"
  3468. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3469. " obj->map_and_fenceable=%d\n",
  3470. i915_gem_obj_offset_view(obj, vm, view->type),
  3471. alignment,
  3472. !!(flags & PIN_MAPPABLE),
  3473. obj->map_and_fenceable);
  3474. ret = i915_vma_unbind(vma);
  3475. if (ret)
  3476. return ret;
  3477. vma = NULL;
  3478. }
  3479. }
  3480. bound = vma ? vma->bound : 0;
  3481. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3482. vma = i915_gem_object_bind_to_vm(obj, vm, alignment,
  3483. flags, view);
  3484. if (IS_ERR(vma))
  3485. return PTR_ERR(vma);
  3486. }
  3487. if (flags & PIN_GLOBAL && !(vma->bound & GLOBAL_BIND)) {
  3488. ret = i915_vma_bind(vma, obj->cache_level, GLOBAL_BIND);
  3489. if (ret)
  3490. return ret;
  3491. }
  3492. if ((bound ^ vma->bound) & GLOBAL_BIND) {
  3493. bool mappable, fenceable;
  3494. u32 fence_size, fence_alignment;
  3495. fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3496. obj->base.size,
  3497. obj->tiling_mode);
  3498. fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3499. obj->base.size,
  3500. obj->tiling_mode,
  3501. true);
  3502. fenceable = (vma->node.size == fence_size &&
  3503. (vma->node.start & (fence_alignment - 1)) == 0);
  3504. mappable = (vma->node.start + obj->base.size <=
  3505. dev_priv->gtt.mappable_end);
  3506. obj->map_and_fenceable = mappable && fenceable;
  3507. }
  3508. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3509. vma->pin_count++;
  3510. if (flags & PIN_MAPPABLE)
  3511. obj->pin_mappable |= true;
  3512. return 0;
  3513. }
  3514. void
  3515. i915_gem_object_ggtt_unpin(struct drm_i915_gem_object *obj)
  3516. {
  3517. struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3518. BUG_ON(!vma);
  3519. BUG_ON(vma->pin_count == 0);
  3520. BUG_ON(!i915_gem_obj_ggtt_bound(obj));
  3521. if (--vma->pin_count == 0)
  3522. obj->pin_mappable = false;
  3523. }
  3524. bool
  3525. i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
  3526. {
  3527. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3528. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3529. struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
  3530. WARN_ON(!ggtt_vma ||
  3531. dev_priv->fence_regs[obj->fence_reg].pin_count >
  3532. ggtt_vma->pin_count);
  3533. dev_priv->fence_regs[obj->fence_reg].pin_count++;
  3534. return true;
  3535. } else
  3536. return false;
  3537. }
  3538. void
  3539. i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
  3540. {
  3541. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3542. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3543. WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
  3544. dev_priv->fence_regs[obj->fence_reg].pin_count--;
  3545. }
  3546. }
  3547. int
  3548. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3549. struct drm_file *file)
  3550. {
  3551. struct drm_i915_gem_busy *args = data;
  3552. struct drm_i915_gem_object *obj;
  3553. int ret;
  3554. ret = i915_mutex_lock_interruptible(dev);
  3555. if (ret)
  3556. return ret;
  3557. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3558. if (&obj->base == NULL) {
  3559. ret = -ENOENT;
  3560. goto unlock;
  3561. }
  3562. /* Count all active objects as busy, even if they are currently not used
  3563. * by the gpu. Users of this interface expect objects to eventually
  3564. * become non-busy without any further actions, therefore emit any
  3565. * necessary flushes here.
  3566. */
  3567. ret = i915_gem_object_flush_active(obj);
  3568. args->busy = obj->active;
  3569. if (obj->last_read_req) {
  3570. struct intel_engine_cs *ring;
  3571. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3572. ring = i915_gem_request_get_ring(obj->last_read_req);
  3573. args->busy |= intel_ring_flag(ring) << 16;
  3574. }
  3575. drm_gem_object_unreference(&obj->base);
  3576. unlock:
  3577. mutex_unlock(&dev->struct_mutex);
  3578. return ret;
  3579. }
  3580. int
  3581. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3582. struct drm_file *file_priv)
  3583. {
  3584. return i915_gem_ring_throttle(dev, file_priv);
  3585. }
  3586. int
  3587. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3588. struct drm_file *file_priv)
  3589. {
  3590. struct drm_i915_private *dev_priv = dev->dev_private;
  3591. struct drm_i915_gem_madvise *args = data;
  3592. struct drm_i915_gem_object *obj;
  3593. int ret;
  3594. switch (args->madv) {
  3595. case I915_MADV_DONTNEED:
  3596. case I915_MADV_WILLNEED:
  3597. break;
  3598. default:
  3599. return -EINVAL;
  3600. }
  3601. ret = i915_mutex_lock_interruptible(dev);
  3602. if (ret)
  3603. return ret;
  3604. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3605. if (&obj->base == NULL) {
  3606. ret = -ENOENT;
  3607. goto unlock;
  3608. }
  3609. if (i915_gem_obj_is_pinned(obj)) {
  3610. ret = -EINVAL;
  3611. goto out;
  3612. }
  3613. if (obj->pages &&
  3614. obj->tiling_mode != I915_TILING_NONE &&
  3615. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3616. if (obj->madv == I915_MADV_WILLNEED)
  3617. i915_gem_object_unpin_pages(obj);
  3618. if (args->madv == I915_MADV_WILLNEED)
  3619. i915_gem_object_pin_pages(obj);
  3620. }
  3621. if (obj->madv != __I915_MADV_PURGED)
  3622. obj->madv = args->madv;
  3623. /* if the object is no longer attached, discard its backing storage */
  3624. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3625. i915_gem_object_truncate(obj);
  3626. args->retained = obj->madv != __I915_MADV_PURGED;
  3627. out:
  3628. drm_gem_object_unreference(&obj->base);
  3629. unlock:
  3630. mutex_unlock(&dev->struct_mutex);
  3631. return ret;
  3632. }
  3633. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3634. const struct drm_i915_gem_object_ops *ops)
  3635. {
  3636. INIT_LIST_HEAD(&obj->global_list);
  3637. INIT_LIST_HEAD(&obj->ring_list);
  3638. INIT_LIST_HEAD(&obj->obj_exec_link);
  3639. INIT_LIST_HEAD(&obj->vma_list);
  3640. INIT_LIST_HEAD(&obj->batch_pool_list);
  3641. obj->ops = ops;
  3642. obj->fence_reg = I915_FENCE_REG_NONE;
  3643. obj->madv = I915_MADV_WILLNEED;
  3644. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3645. }
  3646. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3647. .get_pages = i915_gem_object_get_pages_gtt,
  3648. .put_pages = i915_gem_object_put_pages_gtt,
  3649. };
  3650. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3651. size_t size)
  3652. {
  3653. struct drm_i915_gem_object *obj;
  3654. struct address_space *mapping;
  3655. gfp_t mask;
  3656. obj = i915_gem_object_alloc(dev);
  3657. if (obj == NULL)
  3658. return NULL;
  3659. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3660. i915_gem_object_free(obj);
  3661. return NULL;
  3662. }
  3663. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3664. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3665. /* 965gm cannot relocate objects above 4GiB. */
  3666. mask &= ~__GFP_HIGHMEM;
  3667. mask |= __GFP_DMA32;
  3668. }
  3669. mapping = file_inode(obj->base.filp)->i_mapping;
  3670. mapping_set_gfp_mask(mapping, mask);
  3671. i915_gem_object_init(obj, &i915_gem_object_ops);
  3672. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3673. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3674. if (HAS_LLC(dev)) {
  3675. /* On some devices, we can have the GPU use the LLC (the CPU
  3676. * cache) for about a 10% performance improvement
  3677. * compared to uncached. Graphics requests other than
  3678. * display scanout are coherent with the CPU in
  3679. * accessing this cache. This means in this mode we
  3680. * don't need to clflush on the CPU side, and on the
  3681. * GPU side we only need to flush internal caches to
  3682. * get data visible to the CPU.
  3683. *
  3684. * However, we maintain the display planes as UC, and so
  3685. * need to rebind when first used as such.
  3686. */
  3687. obj->cache_level = I915_CACHE_LLC;
  3688. } else
  3689. obj->cache_level = I915_CACHE_NONE;
  3690. trace_i915_gem_object_create(obj);
  3691. return obj;
  3692. }
  3693. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3694. {
  3695. /* If we are the last user of the backing storage (be it shmemfs
  3696. * pages or stolen etc), we know that the pages are going to be
  3697. * immediately released. In this case, we can then skip copying
  3698. * back the contents from the GPU.
  3699. */
  3700. if (obj->madv != I915_MADV_WILLNEED)
  3701. return false;
  3702. if (obj->base.filp == NULL)
  3703. return true;
  3704. /* At first glance, this looks racy, but then again so would be
  3705. * userspace racing mmap against close. However, the first external
  3706. * reference to the filp can only be obtained through the
  3707. * i915_gem_mmap_ioctl() which safeguards us against the user
  3708. * acquiring such a reference whilst we are in the middle of
  3709. * freeing the object.
  3710. */
  3711. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3712. }
  3713. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3714. {
  3715. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3716. struct drm_device *dev = obj->base.dev;
  3717. struct drm_i915_private *dev_priv = dev->dev_private;
  3718. struct i915_vma *vma, *next;
  3719. intel_runtime_pm_get(dev_priv);
  3720. trace_i915_gem_object_destroy(obj);
  3721. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3722. int ret;
  3723. vma->pin_count = 0;
  3724. ret = i915_vma_unbind(vma);
  3725. if (WARN_ON(ret == -ERESTARTSYS)) {
  3726. bool was_interruptible;
  3727. was_interruptible = dev_priv->mm.interruptible;
  3728. dev_priv->mm.interruptible = false;
  3729. WARN_ON(i915_vma_unbind(vma));
  3730. dev_priv->mm.interruptible = was_interruptible;
  3731. }
  3732. }
  3733. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3734. * before progressing. */
  3735. if (obj->stolen)
  3736. i915_gem_object_unpin_pages(obj);
  3737. WARN_ON(obj->frontbuffer_bits);
  3738. if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  3739. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  3740. obj->tiling_mode != I915_TILING_NONE)
  3741. i915_gem_object_unpin_pages(obj);
  3742. if (WARN_ON(obj->pages_pin_count))
  3743. obj->pages_pin_count = 0;
  3744. if (discard_backing_storage(obj))
  3745. obj->madv = I915_MADV_DONTNEED;
  3746. i915_gem_object_put_pages(obj);
  3747. i915_gem_object_free_mmap_offset(obj);
  3748. BUG_ON(obj->pages);
  3749. if (obj->base.import_attach)
  3750. drm_prime_gem_destroy(&obj->base, NULL);
  3751. if (obj->ops->release)
  3752. obj->ops->release(obj);
  3753. drm_gem_object_release(&obj->base);
  3754. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3755. kfree(obj->bit_17);
  3756. i915_gem_object_free(obj);
  3757. intel_runtime_pm_put(dev_priv);
  3758. }
  3759. struct i915_vma *i915_gem_obj_to_vma_view(struct drm_i915_gem_object *obj,
  3760. struct i915_address_space *vm,
  3761. const struct i915_ggtt_view *view)
  3762. {
  3763. struct i915_vma *vma;
  3764. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3765. if (vma->vm == vm && vma->ggtt_view.type == view->type)
  3766. return vma;
  3767. return NULL;
  3768. }
  3769. void i915_gem_vma_destroy(struct i915_vma *vma)
  3770. {
  3771. struct i915_address_space *vm = NULL;
  3772. WARN_ON(vma->node.allocated);
  3773. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3774. if (!list_empty(&vma->exec_list))
  3775. return;
  3776. vm = vma->vm;
  3777. if (!i915_is_ggtt(vm))
  3778. i915_ppgtt_put(i915_vm_to_ppgtt(vm));
  3779. list_del(&vma->vma_link);
  3780. kfree(vma);
  3781. }
  3782. static void
  3783. i915_gem_stop_ringbuffers(struct drm_device *dev)
  3784. {
  3785. struct drm_i915_private *dev_priv = dev->dev_private;
  3786. struct intel_engine_cs *ring;
  3787. int i;
  3788. for_each_ring(ring, dev_priv, i)
  3789. dev_priv->gt.stop_ring(ring);
  3790. }
  3791. int
  3792. i915_gem_suspend(struct drm_device *dev)
  3793. {
  3794. struct drm_i915_private *dev_priv = dev->dev_private;
  3795. int ret = 0;
  3796. mutex_lock(&dev->struct_mutex);
  3797. ret = i915_gpu_idle(dev);
  3798. if (ret)
  3799. goto err;
  3800. i915_gem_retire_requests(dev);
  3801. /* Under UMS, be paranoid and evict. */
  3802. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3803. i915_gem_evict_everything(dev);
  3804. i915_gem_stop_ringbuffers(dev);
  3805. mutex_unlock(&dev->struct_mutex);
  3806. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3807. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3808. flush_delayed_work(&dev_priv->mm.idle_work);
  3809. /* Assert that we sucessfully flushed all the work and
  3810. * reset the GPU back to its idle, low power state.
  3811. */
  3812. WARN_ON(dev_priv->mm.busy);
  3813. return 0;
  3814. err:
  3815. mutex_unlock(&dev->struct_mutex);
  3816. return ret;
  3817. }
  3818. int i915_gem_l3_remap(struct intel_engine_cs *ring, int slice)
  3819. {
  3820. struct drm_device *dev = ring->dev;
  3821. struct drm_i915_private *dev_priv = dev->dev_private;
  3822. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  3823. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  3824. int i, ret;
  3825. if (!HAS_L3_DPF(dev) || !remap_info)
  3826. return 0;
  3827. ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
  3828. if (ret)
  3829. return ret;
  3830. /*
  3831. * Note: We do not worry about the concurrent register cacheline hang
  3832. * here because no other code should access these registers other than
  3833. * at initialization time.
  3834. */
  3835. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3836. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  3837. intel_ring_emit(ring, reg_base + i);
  3838. intel_ring_emit(ring, remap_info[i/4]);
  3839. }
  3840. intel_ring_advance(ring);
  3841. return ret;
  3842. }
  3843. void i915_gem_init_swizzling(struct drm_device *dev)
  3844. {
  3845. struct drm_i915_private *dev_priv = dev->dev_private;
  3846. if (INTEL_INFO(dev)->gen < 5 ||
  3847. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3848. return;
  3849. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3850. DISP_TILE_SURFACE_SWIZZLING);
  3851. if (IS_GEN5(dev))
  3852. return;
  3853. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3854. if (IS_GEN6(dev))
  3855. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3856. else if (IS_GEN7(dev))
  3857. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3858. else if (IS_GEN8(dev))
  3859. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3860. else
  3861. BUG();
  3862. }
  3863. static bool
  3864. intel_enable_blt(struct drm_device *dev)
  3865. {
  3866. if (!HAS_BLT(dev))
  3867. return false;
  3868. /* The blitter was dysfunctional on early prototypes */
  3869. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3870. DRM_INFO("BLT not supported on this pre-production hardware;"
  3871. " graphics performance will be degraded.\n");
  3872. return false;
  3873. }
  3874. return true;
  3875. }
  3876. static void init_unused_ring(struct drm_device *dev, u32 base)
  3877. {
  3878. struct drm_i915_private *dev_priv = dev->dev_private;
  3879. I915_WRITE(RING_CTL(base), 0);
  3880. I915_WRITE(RING_HEAD(base), 0);
  3881. I915_WRITE(RING_TAIL(base), 0);
  3882. I915_WRITE(RING_START(base), 0);
  3883. }
  3884. static void init_unused_rings(struct drm_device *dev)
  3885. {
  3886. if (IS_I830(dev)) {
  3887. init_unused_ring(dev, PRB1_BASE);
  3888. init_unused_ring(dev, SRB0_BASE);
  3889. init_unused_ring(dev, SRB1_BASE);
  3890. init_unused_ring(dev, SRB2_BASE);
  3891. init_unused_ring(dev, SRB3_BASE);
  3892. } else if (IS_GEN2(dev)) {
  3893. init_unused_ring(dev, SRB0_BASE);
  3894. init_unused_ring(dev, SRB1_BASE);
  3895. } else if (IS_GEN3(dev)) {
  3896. init_unused_ring(dev, PRB1_BASE);
  3897. init_unused_ring(dev, PRB2_BASE);
  3898. }
  3899. }
  3900. int i915_gem_init_rings(struct drm_device *dev)
  3901. {
  3902. struct drm_i915_private *dev_priv = dev->dev_private;
  3903. int ret;
  3904. ret = intel_init_render_ring_buffer(dev);
  3905. if (ret)
  3906. return ret;
  3907. if (HAS_BSD(dev)) {
  3908. ret = intel_init_bsd_ring_buffer(dev);
  3909. if (ret)
  3910. goto cleanup_render_ring;
  3911. }
  3912. if (intel_enable_blt(dev)) {
  3913. ret = intel_init_blt_ring_buffer(dev);
  3914. if (ret)
  3915. goto cleanup_bsd_ring;
  3916. }
  3917. if (HAS_VEBOX(dev)) {
  3918. ret = intel_init_vebox_ring_buffer(dev);
  3919. if (ret)
  3920. goto cleanup_blt_ring;
  3921. }
  3922. if (HAS_BSD2(dev)) {
  3923. ret = intel_init_bsd2_ring_buffer(dev);
  3924. if (ret)
  3925. goto cleanup_vebox_ring;
  3926. }
  3927. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3928. if (ret)
  3929. goto cleanup_bsd2_ring;
  3930. return 0;
  3931. cleanup_bsd2_ring:
  3932. intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
  3933. cleanup_vebox_ring:
  3934. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3935. cleanup_blt_ring:
  3936. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3937. cleanup_bsd_ring:
  3938. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3939. cleanup_render_ring:
  3940. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3941. return ret;
  3942. }
  3943. int
  3944. i915_gem_init_hw(struct drm_device *dev)
  3945. {
  3946. struct drm_i915_private *dev_priv = dev->dev_private;
  3947. struct intel_engine_cs *ring;
  3948. int ret, i;
  3949. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3950. return -EIO;
  3951. if (dev_priv->ellc_size)
  3952. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3953. if (IS_HASWELL(dev))
  3954. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3955. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3956. if (HAS_PCH_NOP(dev)) {
  3957. if (IS_IVYBRIDGE(dev)) {
  3958. u32 temp = I915_READ(GEN7_MSG_CTL);
  3959. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3960. I915_WRITE(GEN7_MSG_CTL, temp);
  3961. } else if (INTEL_INFO(dev)->gen >= 7) {
  3962. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3963. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3964. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3965. }
  3966. }
  3967. i915_gem_init_swizzling(dev);
  3968. /*
  3969. * At least 830 can leave some of the unused rings
  3970. * "active" (ie. head != tail) after resume which
  3971. * will prevent c3 entry. Makes sure all unused rings
  3972. * are totally idle.
  3973. */
  3974. init_unused_rings(dev);
  3975. for_each_ring(ring, dev_priv, i) {
  3976. ret = ring->init_hw(ring);
  3977. if (ret)
  3978. return ret;
  3979. }
  3980. for (i = 0; i < NUM_L3_SLICES(dev); i++)
  3981. i915_gem_l3_remap(&dev_priv->ring[RCS], i);
  3982. /*
  3983. * XXX: Contexts should only be initialized once. Doing a switch to the
  3984. * default context switch however is something we'd like to do after
  3985. * reset or thaw (the latter may not actually be necessary for HW, but
  3986. * goes with our code better). Context switching requires rings (for
  3987. * the do_switch), but before enabling PPGTT. So don't move this.
  3988. */
  3989. ret = i915_gem_context_enable(dev_priv);
  3990. if (ret && ret != -EIO) {
  3991. DRM_ERROR("Context enable failed %d\n", ret);
  3992. i915_gem_cleanup_ringbuffer(dev);
  3993. return ret;
  3994. }
  3995. ret = i915_ppgtt_init_hw(dev);
  3996. if (ret && ret != -EIO) {
  3997. DRM_ERROR("PPGTT enable failed %d\n", ret);
  3998. i915_gem_cleanup_ringbuffer(dev);
  3999. }
  4000. return ret;
  4001. }
  4002. int i915_gem_init(struct drm_device *dev)
  4003. {
  4004. struct drm_i915_private *dev_priv = dev->dev_private;
  4005. int ret;
  4006. i915.enable_execlists = intel_sanitize_enable_execlists(dev,
  4007. i915.enable_execlists);
  4008. mutex_lock(&dev->struct_mutex);
  4009. if (IS_VALLEYVIEW(dev)) {
  4010. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  4011. I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  4012. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  4013. VLV_GTLC_ALLOWWAKEACK), 10))
  4014. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  4015. }
  4016. if (!i915.enable_execlists) {
  4017. dev_priv->gt.do_execbuf = i915_gem_ringbuffer_submission;
  4018. dev_priv->gt.init_rings = i915_gem_init_rings;
  4019. dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
  4020. dev_priv->gt.stop_ring = intel_stop_ring_buffer;
  4021. } else {
  4022. dev_priv->gt.do_execbuf = intel_execlists_submission;
  4023. dev_priv->gt.init_rings = intel_logical_rings_init;
  4024. dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
  4025. dev_priv->gt.stop_ring = intel_logical_ring_stop;
  4026. }
  4027. ret = i915_gem_init_userptr(dev);
  4028. if (ret)
  4029. goto out_unlock;
  4030. i915_gem_init_global_gtt(dev);
  4031. ret = i915_gem_context_init(dev);
  4032. if (ret)
  4033. goto out_unlock;
  4034. ret = dev_priv->gt.init_rings(dev);
  4035. if (ret)
  4036. goto out_unlock;
  4037. ret = i915_gem_init_hw(dev);
  4038. if (ret == -EIO) {
  4039. /* Allow ring initialisation to fail by marking the GPU as
  4040. * wedged. But we only want to do this where the GPU is angry,
  4041. * for all other failure, such as an allocation failure, bail.
  4042. */
  4043. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4044. atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4045. ret = 0;
  4046. }
  4047. out_unlock:
  4048. mutex_unlock(&dev->struct_mutex);
  4049. return ret;
  4050. }
  4051. void
  4052. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4053. {
  4054. struct drm_i915_private *dev_priv = dev->dev_private;
  4055. struct intel_engine_cs *ring;
  4056. int i;
  4057. for_each_ring(ring, dev_priv, i)
  4058. dev_priv->gt.cleanup_ring(ring);
  4059. }
  4060. static void
  4061. init_ring_lists(struct intel_engine_cs *ring)
  4062. {
  4063. INIT_LIST_HEAD(&ring->active_list);
  4064. INIT_LIST_HEAD(&ring->request_list);
  4065. }
  4066. void i915_init_vm(struct drm_i915_private *dev_priv,
  4067. struct i915_address_space *vm)
  4068. {
  4069. if (!i915_is_ggtt(vm))
  4070. drm_mm_init(&vm->mm, vm->start, vm->total);
  4071. vm->dev = dev_priv->dev;
  4072. INIT_LIST_HEAD(&vm->active_list);
  4073. INIT_LIST_HEAD(&vm->inactive_list);
  4074. INIT_LIST_HEAD(&vm->global_link);
  4075. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  4076. }
  4077. void
  4078. i915_gem_load(struct drm_device *dev)
  4079. {
  4080. struct drm_i915_private *dev_priv = dev->dev_private;
  4081. int i;
  4082. dev_priv->slab =
  4083. kmem_cache_create("i915_gem_object",
  4084. sizeof(struct drm_i915_gem_object), 0,
  4085. SLAB_HWCACHE_ALIGN,
  4086. NULL);
  4087. INIT_LIST_HEAD(&dev_priv->vm_list);
  4088. i915_init_vm(dev_priv, &dev_priv->gtt.base);
  4089. INIT_LIST_HEAD(&dev_priv->context_list);
  4090. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4091. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4092. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4093. for (i = 0; i < I915_NUM_RINGS; i++)
  4094. init_ring_lists(&dev_priv->ring[i]);
  4095. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4096. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4097. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4098. i915_gem_retire_work_handler);
  4099. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4100. i915_gem_idle_work_handler);
  4101. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4102. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4103. if (!drm_core_check_feature(dev, DRIVER_MODESET) && IS_GEN3(dev)) {
  4104. I915_WRITE(MI_ARB_STATE,
  4105. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  4106. }
  4107. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4108. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4109. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4110. dev_priv->fence_reg_start = 3;
  4111. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4112. dev_priv->num_fence_regs = 32;
  4113. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4114. dev_priv->num_fence_regs = 16;
  4115. else
  4116. dev_priv->num_fence_regs = 8;
  4117. /* Initialize fence registers to zero */
  4118. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4119. i915_gem_restore_fences(dev);
  4120. i915_gem_detect_bit_6_swizzle(dev);
  4121. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4122. dev_priv->mm.interruptible = true;
  4123. dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
  4124. dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
  4125. dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
  4126. register_shrinker(&dev_priv->mm.shrinker);
  4127. dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  4128. register_oom_notifier(&dev_priv->mm.oom_notifier);
  4129. i915_gem_batch_pool_init(dev, &dev_priv->mm.batch_pool);
  4130. mutex_init(&dev_priv->fb_tracking.lock);
  4131. }
  4132. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4133. {
  4134. struct drm_i915_file_private *file_priv = file->driver_priv;
  4135. cancel_delayed_work_sync(&file_priv->mm.idle_work);
  4136. /* Clean up our request list when the client is going away, so that
  4137. * later retire_requests won't dereference our soon-to-be-gone
  4138. * file_priv.
  4139. */
  4140. spin_lock(&file_priv->mm.lock);
  4141. while (!list_empty(&file_priv->mm.request_list)) {
  4142. struct drm_i915_gem_request *request;
  4143. request = list_first_entry(&file_priv->mm.request_list,
  4144. struct drm_i915_gem_request,
  4145. client_list);
  4146. list_del(&request->client_list);
  4147. request->file_priv = NULL;
  4148. }
  4149. spin_unlock(&file_priv->mm.lock);
  4150. }
  4151. static void
  4152. i915_gem_file_idle_work_handler(struct work_struct *work)
  4153. {
  4154. struct drm_i915_file_private *file_priv =
  4155. container_of(work, typeof(*file_priv), mm.idle_work.work);
  4156. atomic_set(&file_priv->rps_wait_boost, false);
  4157. }
  4158. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4159. {
  4160. struct drm_i915_file_private *file_priv;
  4161. int ret;
  4162. DRM_DEBUG_DRIVER("\n");
  4163. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4164. if (!file_priv)
  4165. return -ENOMEM;
  4166. file->driver_priv = file_priv;
  4167. file_priv->dev_priv = dev->dev_private;
  4168. file_priv->file = file;
  4169. spin_lock_init(&file_priv->mm.lock);
  4170. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4171. INIT_DELAYED_WORK(&file_priv->mm.idle_work,
  4172. i915_gem_file_idle_work_handler);
  4173. ret = i915_gem_context_open(dev, file);
  4174. if (ret)
  4175. kfree(file_priv);
  4176. return ret;
  4177. }
  4178. /**
  4179. * i915_gem_track_fb - update frontbuffer tracking
  4180. * old: current GEM buffer for the frontbuffer slots
  4181. * new: new GEM buffer for the frontbuffer slots
  4182. * frontbuffer_bits: bitmask of frontbuffer slots
  4183. *
  4184. * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4185. * from @old and setting them in @new. Both @old and @new can be NULL.
  4186. */
  4187. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4188. struct drm_i915_gem_object *new,
  4189. unsigned frontbuffer_bits)
  4190. {
  4191. if (old) {
  4192. WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4193. WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4194. old->frontbuffer_bits &= ~frontbuffer_bits;
  4195. }
  4196. if (new) {
  4197. WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4198. WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4199. new->frontbuffer_bits |= frontbuffer_bits;
  4200. }
  4201. }
  4202. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  4203. {
  4204. if (!mutex_is_locked(mutex))
  4205. return false;
  4206. #if defined(CONFIG_SMP) && !defined(CONFIG_DEBUG_MUTEXES)
  4207. return mutex->owner == task;
  4208. #else
  4209. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  4210. return false;
  4211. #endif
  4212. }
  4213. static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
  4214. {
  4215. if (!mutex_trylock(&dev->struct_mutex)) {
  4216. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  4217. return false;
  4218. if (to_i915(dev)->mm.shrinker_no_lock_stealing)
  4219. return false;
  4220. *unlock = false;
  4221. } else
  4222. *unlock = true;
  4223. return true;
  4224. }
  4225. static int num_vma_bound(struct drm_i915_gem_object *obj)
  4226. {
  4227. struct i915_vma *vma;
  4228. int count = 0;
  4229. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4230. if (drm_mm_node_allocated(&vma->node))
  4231. count++;
  4232. return count;
  4233. }
  4234. static unsigned long
  4235. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  4236. {
  4237. struct drm_i915_private *dev_priv =
  4238. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  4239. struct drm_device *dev = dev_priv->dev;
  4240. struct drm_i915_gem_object *obj;
  4241. unsigned long count;
  4242. bool unlock;
  4243. if (!i915_gem_shrinker_lock(dev, &unlock))
  4244. return 0;
  4245. count = 0;
  4246. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  4247. if (obj->pages_pin_count == 0)
  4248. count += obj->base.size >> PAGE_SHIFT;
  4249. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4250. if (!i915_gem_obj_is_pinned(obj) &&
  4251. obj->pages_pin_count == num_vma_bound(obj))
  4252. count += obj->base.size >> PAGE_SHIFT;
  4253. }
  4254. if (unlock)
  4255. mutex_unlock(&dev->struct_mutex);
  4256. return count;
  4257. }
  4258. /* All the new VM stuff */
  4259. unsigned long i915_gem_obj_offset_view(struct drm_i915_gem_object *o,
  4260. struct i915_address_space *vm,
  4261. enum i915_ggtt_view_type view)
  4262. {
  4263. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4264. struct i915_vma *vma;
  4265. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4266. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4267. if (vma->vm == vm && vma->ggtt_view.type == view)
  4268. return vma->node.start;
  4269. }
  4270. WARN(1, "%s vma for this object not found.\n",
  4271. i915_is_ggtt(vm) ? "global" : "ppgtt");
  4272. return -1;
  4273. }
  4274. bool i915_gem_obj_bound_view(struct drm_i915_gem_object *o,
  4275. struct i915_address_space *vm,
  4276. enum i915_ggtt_view_type view)
  4277. {
  4278. struct i915_vma *vma;
  4279. list_for_each_entry(vma, &o->vma_list, vma_link)
  4280. if (vma->vm == vm &&
  4281. vma->ggtt_view.type == view &&
  4282. drm_mm_node_allocated(&vma->node))
  4283. return true;
  4284. return false;
  4285. }
  4286. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4287. {
  4288. struct i915_vma *vma;
  4289. list_for_each_entry(vma, &o->vma_list, vma_link)
  4290. if (drm_mm_node_allocated(&vma->node))
  4291. return true;
  4292. return false;
  4293. }
  4294. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4295. struct i915_address_space *vm)
  4296. {
  4297. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4298. struct i915_vma *vma;
  4299. WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4300. BUG_ON(list_empty(&o->vma_list));
  4301. list_for_each_entry(vma, &o->vma_list, vma_link)
  4302. if (vma->vm == vm)
  4303. return vma->node.size;
  4304. return 0;
  4305. }
  4306. static unsigned long
  4307. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  4308. {
  4309. struct drm_i915_private *dev_priv =
  4310. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  4311. struct drm_device *dev = dev_priv->dev;
  4312. unsigned long freed;
  4313. bool unlock;
  4314. if (!i915_gem_shrinker_lock(dev, &unlock))
  4315. return SHRINK_STOP;
  4316. freed = i915_gem_shrink(dev_priv,
  4317. sc->nr_to_scan,
  4318. I915_SHRINK_BOUND |
  4319. I915_SHRINK_UNBOUND |
  4320. I915_SHRINK_PURGEABLE);
  4321. if (freed < sc->nr_to_scan)
  4322. freed += i915_gem_shrink(dev_priv,
  4323. sc->nr_to_scan - freed,
  4324. I915_SHRINK_BOUND |
  4325. I915_SHRINK_UNBOUND);
  4326. if (unlock)
  4327. mutex_unlock(&dev->struct_mutex);
  4328. return freed;
  4329. }
  4330. static int
  4331. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  4332. {
  4333. struct drm_i915_private *dev_priv =
  4334. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  4335. struct drm_device *dev = dev_priv->dev;
  4336. struct drm_i915_gem_object *obj;
  4337. unsigned long timeout = msecs_to_jiffies(5000) + 1;
  4338. unsigned long pinned, bound, unbound, freed_pages;
  4339. bool was_interruptible;
  4340. bool unlock;
  4341. while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout) {
  4342. schedule_timeout_killable(1);
  4343. if (fatal_signal_pending(current))
  4344. return NOTIFY_DONE;
  4345. }
  4346. if (timeout == 0) {
  4347. pr_err("Unable to purge GPU memory due lock contention.\n");
  4348. return NOTIFY_DONE;
  4349. }
  4350. was_interruptible = dev_priv->mm.interruptible;
  4351. dev_priv->mm.interruptible = false;
  4352. freed_pages = i915_gem_shrink_all(dev_priv);
  4353. dev_priv->mm.interruptible = was_interruptible;
  4354. /* Because we may be allocating inside our own driver, we cannot
  4355. * assert that there are no objects with pinned pages that are not
  4356. * being pointed to by hardware.
  4357. */
  4358. unbound = bound = pinned = 0;
  4359. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4360. if (!obj->base.filp) /* not backed by a freeable object */
  4361. continue;
  4362. if (obj->pages_pin_count)
  4363. pinned += obj->base.size;
  4364. else
  4365. unbound += obj->base.size;
  4366. }
  4367. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4368. if (!obj->base.filp)
  4369. continue;
  4370. if (obj->pages_pin_count)
  4371. pinned += obj->base.size;
  4372. else
  4373. bound += obj->base.size;
  4374. }
  4375. if (unlock)
  4376. mutex_unlock(&dev->struct_mutex);
  4377. if (freed_pages || unbound || bound)
  4378. pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
  4379. freed_pages << PAGE_SHIFT, pinned);
  4380. if (unbound || bound)
  4381. pr_err("%lu and %lu bytes still available in the "
  4382. "bound and unbound GPU page lists.\n",
  4383. bound, unbound);
  4384. *(unsigned long *)ptr += freed_pages;
  4385. return NOTIFY_DONE;
  4386. }
  4387. struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj)
  4388. {
  4389. struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
  4390. struct i915_vma *vma;
  4391. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4392. if (vma->vm == ggtt &&
  4393. vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
  4394. return vma;
  4395. return NULL;
  4396. }